AOMedia AV1 Codec
svc_encoder_rtc
1/*
2 * Copyright (c) 2019, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12// This is an example demonstrating how to implement a multi-layer AOM
13// encoding scheme for RTC video applications.
14
15#include <assert.h>
16#include <inttypes.h>
17#include <limits.h>
18#include <math.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include <memory>
24
25#include "config/aom_config.h"
26
27#if CONFIG_AV1_DECODER
28#include "aom/aom_decoder.h"
29#endif
30#include "aom/aom_encoder.h"
31#include "aom/aom_image.h"
32#include "aom/aom_integer.h"
33#include "aom/aomcx.h"
34#include "aom_dsp/bitwriter_buffer.h"
35#include "aom_ports/aom_timer.h"
36#include "av1/ratectrl_rtc.h"
37#include "common/args.h"
38#include "common/tools_common.h"
39#include "common/video_writer.h"
40#include "examples/encoder_util.h"
41#include "examples/multilayer_metadata.h"
42
43#define OPTION_BUFFER_SIZE 1024
44#define MAX_NUM_SPATIAL_LAYERS 4
45
46typedef struct {
47 const char *output_filename;
48 char options[OPTION_BUFFER_SIZE];
49 struct AvxInputContext input_ctx[MAX_NUM_SPATIAL_LAYERS];
50 int speed;
51 int aq_mode;
52 int layering_mode;
53 int output_obu;
54 int decode;
55 int tune_content;
56 int show_psnr;
57 bool use_external_rc;
58 bool scale_factors_explicitly_set;
59 const char *multilayer_metadata_file;
60} AppInput;
61
62typedef enum {
63 QUANTIZER = 0,
64 BITRATE,
65 SCALE_FACTOR,
66 AUTO_ALT_REF,
67 ALL_OPTION_TYPES
68} LAYER_OPTION_TYPE;
69
70static const arg_def_t outputfile =
71 ARG_DEF("o", "output", 1, "Output filename");
72static const arg_def_t frames_arg =
73 ARG_DEF("f", "frames", 1, "Number of frames to encode");
74static const arg_def_t threads_arg =
75 ARG_DEF("th", "threads", 1, "Number of threads to use");
76static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "Source width");
77static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "Source height");
78static const arg_def_t timebase_arg =
79 ARG_DEF("t", "timebase", 1, "Timebase (num/den)");
80static const arg_def_t bitrate_arg = ARG_DEF(
81 "b", "target-bitrate", 1, "Encoding bitrate, in kilobits per second");
82static const arg_def_t spatial_layers_arg =
83 ARG_DEF("sl", "spatial-layers", 1, "Number of spatial SVC layers");
84static const arg_def_t temporal_layers_arg =
85 ARG_DEF("tl", "temporal-layers", 1, "Number of temporal SVC layers");
86static const arg_def_t layering_mode_arg =
87 ARG_DEF("lm", "layering-mode", 1, "Temporal layering scheme.");
88static const arg_def_t kf_dist_arg =
89 ARG_DEF("k", "kf-dist", 1, "Number of frames between keyframes");
90static const arg_def_t scale_factors_arg =
91 ARG_DEF("r", "scale-factors", 1, "Scale factors (lowest to highest layer)");
92static const arg_def_t min_q_arg =
93 ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
94static const arg_def_t max_q_arg =
95 ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
96static const arg_def_t speed_arg =
97 ARG_DEF("sp", "speed", 1, "Speed configuration");
98static const arg_def_t aqmode_arg =
99 ARG_DEF("aq", "aqmode", 1, "AQ mode off/on");
100static const arg_def_t bitrates_arg =
101 ARG_DEF("bl", "bitrates", 1,
102 "Bitrates[spatial_layer * num_temporal_layer + temporal_layer]");
103static const arg_def_t dropframe_thresh_arg =
104 ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
105static const arg_def_t error_resilient_arg =
106 ARG_DEF(NULL, "error-resilient", 1, "Error resilient flag");
107static const arg_def_t output_obu_arg =
108 ARG_DEF(NULL, "output-obu", 1,
109 "Write OBUs when set to 1. Otherwise write IVF files.");
110static const arg_def_t test_decode_arg =
111 ARG_DEF(NULL, "test-decode", 1,
112 "Attempt to test decoding the output when set to 1. Default is 1.");
113static const arg_def_t psnr_arg =
114 ARG_DEF(NULL, "psnr", -1, "Show PSNR in status line.");
115static const arg_def_t ext_rc_arg =
116 ARG_DEF(NULL, "use-ext-rc", 0, "Use external rate control.");
117static const struct arg_enum_list tune_content_enum[] = {
118 { "default", AOM_CONTENT_DEFAULT },
119 { "screen", AOM_CONTENT_SCREEN },
120 { "film", AOM_CONTENT_FILM },
121 { NULL, 0 }
122};
123static const arg_def_t tune_content_arg = ARG_DEF_ENUM(
124 NULL, "tune-content", 1, "Tune content type", tune_content_enum);
125#if CONFIG_CWG_E050
126static const arg_def_t multilayer_metadata_file_arg =
127 ARG_DEF("ml", "multilayer_metadata_file", 1,
128 "Experimental: path to multilayer metadata file");
129#endif
130
131#if CONFIG_AV1_HIGHBITDEPTH
132static const struct arg_enum_list bitdepth_enum[] = { { "8", AOM_BITS_8 },
133 { "10", AOM_BITS_10 },
134 { NULL, 0 } };
135
136static const arg_def_t bitdepth_arg = ARG_DEF_ENUM(
137 "d", "bit-depth", 1, "Bit depth for codec 8 or 10. ", bitdepth_enum);
138#endif // CONFIG_AV1_HIGHBITDEPTH
139
140static const arg_def_t *svc_args[] = {
141 &frames_arg,
142 &outputfile,
143 &width_arg,
144 &height_arg,
145 &timebase_arg,
146 &bitrate_arg,
147 &spatial_layers_arg,
148 &kf_dist_arg,
149 &scale_factors_arg,
150 &min_q_arg,
151 &max_q_arg,
152 &temporal_layers_arg,
153 &layering_mode_arg,
154 &threads_arg,
155 &aqmode_arg,
156#if CONFIG_AV1_HIGHBITDEPTH
157 &bitdepth_arg,
158#endif
159 &speed_arg,
160 &bitrates_arg,
161 &dropframe_thresh_arg,
162 &error_resilient_arg,
163 &output_obu_arg,
164 &test_decode_arg,
165 &tune_content_arg,
166 &psnr_arg,
167#if CONFIG_CWG_E050
168 &multilayer_metadata_file_arg,
169#endif
170 NULL,
171};
172
173#define zero(Dest) memset(&(Dest), 0, sizeof(Dest))
174
175static const char *exec_name;
176
177void usage_exit(void) {
178 fprintf(stderr,
179 "Usage: %s <options> input_filename [input_filename ...] -o "
180 "output_filename\n",
181 exec_name);
182 fprintf(stderr, "Options:\n");
183 arg_show_usage(stderr, svc_args);
184 fprintf(
185 stderr,
186 "Input files must be y4m or yuv.\n"
187 "If multiple input files are specified, they correspond to spatial "
188 "layers, and there should be as many as there are spatial layers.\n"
189 "All input files must have the same width, height, frame rate and number "
190 "of frames.\n"
191 "If only one file is specified, it is used for all spatial layers.\n");
192 exit(EXIT_FAILURE);
193}
194
195static int file_is_y4m(const char detect[4]) {
196 return memcmp(detect, "YUV4", 4) == 0;
197}
198
199static int fourcc_is_ivf(const char detect[4]) {
200 if (memcmp(detect, "DKIF", 4) == 0) {
201 return 1;
202 }
203 return 0;
204}
205
206static const int option_max_values[ALL_OPTION_TYPES] = { 63, INT_MAX, INT_MAX,
207 1 };
208
209static const int option_min_values[ALL_OPTION_TYPES] = { 0, 0, 1, 0 };
210
211static void open_input_file(struct AvxInputContext *input,
213 /* Parse certain options from the input file, if possible */
214 input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
215 : set_binary_mode(stdin);
216
217 if (!input->file) fatal("Failed to open input file");
218
219 if (!fseeko(input->file, 0, SEEK_END)) {
220 /* Input file is seekable. Figure out how long it is, so we can get
221 * progress info.
222 */
223 input->length = ftello(input->file);
224 rewind(input->file);
225 }
226
227 /* Default to 1:1 pixel aspect ratio. */
228 input->pixel_aspect_ratio.numerator = 1;
229 input->pixel_aspect_ratio.denominator = 1;
230
231 /* For RAW input sources, these bytes will applied on the first frame
232 * in read_frame().
233 */
234 input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
235 input->detect.position = 0;
236
237 if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
238 if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
239 input->only_i420) >= 0) {
240 input->file_type = FILE_TYPE_Y4M;
241 input->width = input->y4m.pic_w;
242 input->height = input->y4m.pic_h;
243 input->pixel_aspect_ratio.numerator = input->y4m.par_n;
244 input->pixel_aspect_ratio.denominator = input->y4m.par_d;
245 input->framerate.numerator = input->y4m.fps_n;
246 input->framerate.denominator = input->y4m.fps_d;
247 input->fmt = input->y4m.aom_fmt;
248 input->bit_depth = static_cast<aom_bit_depth_t>(input->y4m.bit_depth);
249 } else {
250 fatal("Unsupported Y4M stream.");
251 }
252 } else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
253 fatal("IVF is not supported as input.");
254 } else {
255 input->file_type = FILE_TYPE_RAW;
256 }
257}
258
259static aom_codec_err_t extract_option(LAYER_OPTION_TYPE type, char *input,
260 int *value0, int *value1) {
261 if (type == SCALE_FACTOR) {
262 *value0 = (int)strtol(input, &input, 10);
263 if (*input++ != '/') return AOM_CODEC_INVALID_PARAM;
264 *value1 = (int)strtol(input, &input, 10);
265
266 if (*value0 < option_min_values[SCALE_FACTOR] ||
267 *value1 < option_min_values[SCALE_FACTOR] ||
268 *value0 > option_max_values[SCALE_FACTOR] ||
269 *value1 > option_max_values[SCALE_FACTOR] ||
270 *value0 > *value1) // num shouldn't be greater than den
272 } else {
273 *value0 = atoi(input);
274 if (*value0 < option_min_values[type] || *value0 > option_max_values[type])
276 }
277 return AOM_CODEC_OK;
278}
279
280static aom_codec_err_t parse_layer_options_from_string(
281 aom_svc_params_t *svc_params, LAYER_OPTION_TYPE type, const char *input,
282 int *option0, int *option1) {
284 char *input_string;
285 char *token;
286 const char *delim = ",";
287 int num_layers = svc_params->number_spatial_layers;
288 int i = 0;
289
290 if (type == BITRATE)
291 num_layers =
292 svc_params->number_spatial_layers * svc_params->number_temporal_layers;
293
294 if (input == NULL || option0 == NULL ||
295 (option1 == NULL && type == SCALE_FACTOR))
297
298 const size_t input_length = strlen(input);
299 input_string = reinterpret_cast<char *>(malloc(input_length + 1));
300 if (input_string == NULL) return AOM_CODEC_MEM_ERROR;
301 memcpy(input_string, input, input_length + 1);
302 token = strtok(input_string, delim); // NOLINT
303 for (i = 0; i < num_layers; ++i) {
304 if (token != NULL) {
305 res = extract_option(type, token, option0 + i, option1 + i);
306 if (res != AOM_CODEC_OK) break;
307 token = strtok(NULL, delim); // NOLINT
308 } else {
310 break;
311 }
312 }
313 free(input_string);
314 return res;
315}
316
317static void parse_command_line(int argc, const char **argv_,
318 AppInput *app_input,
319 aom_svc_params_t *svc_params,
320 aom_codec_enc_cfg_t *enc_cfg) {
321 struct arg arg;
322 char **argv = NULL;
323 char **argi = NULL;
324 char **argj = NULL;
325 char string_options[1024] = { 0 };
326
327 // Default settings
328 svc_params->number_spatial_layers = 1;
329 svc_params->number_temporal_layers = 1;
330 app_input->layering_mode = 0;
331 app_input->output_obu = 0;
332 app_input->decode = 1;
333 enc_cfg->g_threads = 1;
334 enc_cfg->rc_end_usage = AOM_CBR;
335
336 // process command line options
337 argv = argv_dup(argc - 1, argv_ + 1);
338 if (!argv) {
339 fprintf(stderr, "Error allocating argument list\n");
340 exit(EXIT_FAILURE);
341 }
342 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
343 arg.argv_step = 1;
344
345 if (arg_match(&arg, &outputfile, argi)) {
346 app_input->output_filename = arg.val;
347 } else if (arg_match(&arg, &width_arg, argi)) {
348 enc_cfg->g_w = arg_parse_uint(&arg);
349 } else if (arg_match(&arg, &height_arg, argi)) {
350 enc_cfg->g_h = arg_parse_uint(&arg);
351 } else if (arg_match(&arg, &timebase_arg, argi)) {
352 enc_cfg->g_timebase = arg_parse_rational(&arg);
353 } else if (arg_match(&arg, &bitrate_arg, argi)) {
354 enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
355 } else if (arg_match(&arg, &spatial_layers_arg, argi)) {
356 svc_params->number_spatial_layers = arg_parse_uint(&arg);
357 } else if (arg_match(&arg, &temporal_layers_arg, argi)) {
358 svc_params->number_temporal_layers = arg_parse_uint(&arg);
359 } else if (arg_match(&arg, &speed_arg, argi)) {
360 app_input->speed = arg_parse_uint(&arg);
361 if (app_input->speed > 11) {
362 aom_tools_warn("Mapping speed %d to speed 11.\n", app_input->speed);
363 }
364 } else if (arg_match(&arg, &aqmode_arg, argi)) {
365 app_input->aq_mode = arg_parse_uint(&arg);
366 } else if (arg_match(&arg, &threads_arg, argi)) {
367 enc_cfg->g_threads = arg_parse_uint(&arg);
368 } else if (arg_match(&arg, &layering_mode_arg, argi)) {
369 app_input->layering_mode = arg_parse_int(&arg);
370 } else if (arg_match(&arg, &kf_dist_arg, argi)) {
371 enc_cfg->kf_min_dist = arg_parse_uint(&arg);
372 enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
373 } else if (arg_match(&arg, &scale_factors_arg, argi)) {
374 aom_codec_err_t res = parse_layer_options_from_string(
375 svc_params, SCALE_FACTOR, arg.val, svc_params->scaling_factor_num,
376 svc_params->scaling_factor_den);
377 app_input->scale_factors_explicitly_set = true;
378 if (res != AOM_CODEC_OK) {
379 die("Failed to parse scale factors: %s\n",
381 }
382 } else if (arg_match(&arg, &min_q_arg, argi)) {
383 enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
384 } else if (arg_match(&arg, &max_q_arg, argi)) {
385 enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
386#if CONFIG_AV1_HIGHBITDEPTH
387 } else if (arg_match(&arg, &bitdepth_arg, argi)) {
388 enc_cfg->g_bit_depth =
389 static_cast<aom_bit_depth_t>(arg_parse_enum_or_int(&arg));
390 switch (enc_cfg->g_bit_depth) {
391 case AOM_BITS_8:
392 enc_cfg->g_input_bit_depth = 8;
393 enc_cfg->g_profile = 0;
394 break;
395 case AOM_BITS_10:
396 enc_cfg->g_input_bit_depth = 10;
397 enc_cfg->g_profile = 0;
398 break;
399 default:
400 die("Error: Invalid bit depth selected (%d)\n", enc_cfg->g_bit_depth);
401 }
402#endif // CONFIG_VP9_HIGHBITDEPTH
403 } else if (arg_match(&arg, &dropframe_thresh_arg, argi)) {
404 enc_cfg->rc_dropframe_thresh = arg_parse_uint(&arg);
405 } else if (arg_match(&arg, &error_resilient_arg, argi)) {
406 enc_cfg->g_error_resilient = arg_parse_uint(&arg);
407 if (enc_cfg->g_error_resilient != 0 && enc_cfg->g_error_resilient != 1)
408 die("Invalid value for error resilient (0, 1): %d.",
409 enc_cfg->g_error_resilient);
410 } else if (arg_match(&arg, &output_obu_arg, argi)) {
411 app_input->output_obu = arg_parse_uint(&arg);
412 if (app_input->output_obu != 0 && app_input->output_obu != 1)
413 die("Invalid value for obu output flag (0, 1): %d.",
414 app_input->output_obu);
415 } else if (arg_match(&arg, &test_decode_arg, argi)) {
416 app_input->decode = arg_parse_uint(&arg);
417 if (app_input->decode != 0 && app_input->decode != 1)
418 die("Invalid value for test decode flag (0, 1): %d.",
419 app_input->decode);
420 } else if (arg_match(&arg, &tune_content_arg, argi)) {
421 app_input->tune_content = arg_parse_enum_or_int(&arg);
422 printf("tune content %d\n", app_input->tune_content);
423 } else if (arg_match(&arg, &psnr_arg, argi)) {
424 app_input->show_psnr = 1;
425 } else if (arg_match(&arg, &ext_rc_arg, argi)) {
426 app_input->use_external_rc = true;
427#if CONFIG_CWG_E050
428 } else if (arg_match(&arg, &multilayer_metadata_file_arg, argi)) {
429 app_input->multilayer_metadata_file = arg.val;
430#endif
431 } else {
432 ++argj;
433 }
434 }
435
436 // Total bitrate needs to be parsed after the number of layers.
437 for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
438 arg.argv_step = 1;
439 if (arg_match(&arg, &bitrates_arg, argi)) {
440 aom_codec_err_t res = parse_layer_options_from_string(
441 svc_params, BITRATE, arg.val, svc_params->layer_target_bitrate, NULL);
442 if (res != AOM_CODEC_OK) {
443 die("Failed to parse bitrates: %s\n", aom_codec_err_to_string(res));
444 }
445 } else {
446 ++argj;
447 }
448 }
449
450 // There will be a space in front of the string options
451 if (strlen(string_options) > 0)
452 strncpy(app_input->options, string_options, OPTION_BUFFER_SIZE);
453
454 // Check for unrecognized options
455 for (argi = argv; *argi; ++argi)
456 if (argi[0][0] == '-' && strlen(argi[0]) > 1)
457 die("Error: Unrecognized option %s\n", *argi);
458
459 if (argv[0] == NULL) {
460 usage_exit();
461 }
462
463 int input_count = 0;
464 while (argv[input_count] != NULL && input_count < MAX_NUM_SPATIAL_LAYERS) {
465 app_input->input_ctx[input_count].filename = argv[input_count];
466 ++input_count;
467 }
468 if (input_count > 1 && input_count != svc_params->number_spatial_layers) {
469 die("Error: Number of input files does not match number of spatial layers");
470 }
471 if (argv[input_count] != NULL) {
472 die("Error: Too many input files specified, there should be at most %d",
473 MAX_NUM_SPATIAL_LAYERS);
474 }
475
476 free(argv);
477
478 for (int i = 0; i < input_count; ++i) {
479 open_input_file(&app_input->input_ctx[i], AOM_CSP_UNKNOWN);
480 if (app_input->input_ctx[i].file_type == FILE_TYPE_Y4M) {
481 if (enc_cfg->g_w == 0 || enc_cfg->g_h == 0) {
482 // Override these settings with the info from Y4M file.
483 enc_cfg->g_w = app_input->input_ctx[i].width;
484 enc_cfg->g_h = app_input->input_ctx[i].height;
485 // g_timebase is the reciprocal of frame rate.
486 enc_cfg->g_timebase.num = app_input->input_ctx[i].framerate.denominator;
487 enc_cfg->g_timebase.den = app_input->input_ctx[i].framerate.numerator;
488 } else if (enc_cfg->g_w != app_input->input_ctx[i].width ||
489 enc_cfg->g_h != app_input->input_ctx[i].height ||
490 enc_cfg->g_timebase.num !=
491 app_input->input_ctx[i].framerate.denominator ||
492 enc_cfg->g_timebase.den !=
493 app_input->input_ctx[i].framerate.numerator) {
494 die("Error: Input file dimensions and/or frame rate mismatch");
495 }
496 }
497 }
498 if (enc_cfg->g_w == 0 || enc_cfg->g_h == 0) {
499 die("Error: Input file dimensions not set, use -w and -h");
500 }
501
502 if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
503 enc_cfg->g_h % 2)
504 die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
505
506 printf(
507 "Codec %s\n"
508 "layers: %d\n"
509 "width %u, height: %u\n"
510 "num: %d, den: %d, bitrate: %u\n"
511 "gop size: %u\n",
513 svc_params->number_spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
514 enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
515 enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
516}
517
518static const int mode_to_num_temporal_layers[12] = {
519 1, 2, 3, 3, 2, 1, 1, 3, 3, 3, 3, 3,
520};
521static const int mode_to_num_spatial_layers[12] = {
522 1, 1, 1, 1, 1, 2, 3, 2, 3, 3, 3, 3,
523};
524
525// For rate control encoding stats.
526struct RateControlMetrics {
527 // Number of input frames per layer.
528 int layer_input_frames[AOM_MAX_TS_LAYERS];
529 // Number of encoded non-key frames per layer.
530 int layer_enc_frames[AOM_MAX_TS_LAYERS];
531 // Framerate per layer layer (cumulative).
532 double layer_framerate[AOM_MAX_TS_LAYERS];
533 // Target average frame size per layer (per-frame-bandwidth per layer).
534 double layer_pfb[AOM_MAX_LAYERS];
535 // Actual average frame size per layer.
536 double layer_avg_frame_size[AOM_MAX_LAYERS];
537 // Average rate mismatch per layer (|target - actual| / target).
538 double layer_avg_rate_mismatch[AOM_MAX_LAYERS];
539 // Actual encoding bitrate per layer (cumulative across temporal layers).
540 double layer_encoding_bitrate[AOM_MAX_LAYERS];
541 // Average of the short-time encoder actual bitrate.
542 // TODO(marpan): Should we add these short-time stats for each layer?
543 double avg_st_encoding_bitrate;
544 // Variance of the short-time encoder actual bitrate.
545 double variance_st_encoding_bitrate;
546 // Window (number of frames) for computing short-timee encoding bitrate.
547 int window_size;
548 // Number of window measurements.
549 int window_count;
550 int layer_target_bitrate[AOM_MAX_LAYERS];
551};
552
553static const int REF_FRAMES = 8;
554
555static const int INTER_REFS_PER_FRAME = 7;
556
557// Reference frames used in this example encoder.
558enum {
559 SVC_LAST_FRAME = 0,
560 SVC_LAST2_FRAME,
561 SVC_LAST3_FRAME,
562 SVC_GOLDEN_FRAME,
563 SVC_BWDREF_FRAME,
564 SVC_ALTREF2_FRAME,
565 SVC_ALTREF_FRAME
566};
567
568static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
569 FILE *f = input_ctx->file;
570 y4m_input *y4m = &input_ctx->y4m;
571 int shortread = 0;
572
573 if (input_ctx->file_type == FILE_TYPE_Y4M) {
574 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
575 } else {
576 shortread = read_yuv_frame(input_ctx, img);
577 }
578
579 return !shortread;
580}
581
582static void close_input_file(struct AvxInputContext *input) {
583 fclose(input->file);
584 if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
585}
586
587// Note: these rate control metrics assume only 1 key frame in the
588// sequence (i.e., first frame only). So for temporal pattern# 7
589// (which has key frame for every frame on base layer), the metrics
590// computation will be off/wrong.
591// TODO(marpan): Update these metrics to account for multiple key frames
592// in the stream.
593static void set_rate_control_metrics(struct RateControlMetrics *rc,
594 double framerate, int ss_number_layers,
595 int ts_number_layers) {
596 int ts_rate_decimator[AOM_MAX_TS_LAYERS] = { 1 };
597 ts_rate_decimator[0] = 1;
598 if (ts_number_layers == 2) {
599 ts_rate_decimator[0] = 2;
600 ts_rate_decimator[1] = 1;
601 }
602 if (ts_number_layers == 3) {
603 ts_rate_decimator[0] = 4;
604 ts_rate_decimator[1] = 2;
605 ts_rate_decimator[2] = 1;
606 }
607 // Set the layer (cumulative) framerate and the target layer (non-cumulative)
608 // per-frame-bandwidth, for the rate control encoding stats below.
609 for (int sl = 0; sl < ss_number_layers; ++sl) {
610 int i = sl * ts_number_layers;
611 rc->layer_framerate[0] = framerate / ts_rate_decimator[0];
612 rc->layer_pfb[i] =
613 1000.0 * rc->layer_target_bitrate[i] / rc->layer_framerate[0];
614 for (int tl = 0; tl < ts_number_layers; ++tl) {
615 i = sl * ts_number_layers + tl;
616 if (tl > 0) {
617 rc->layer_framerate[tl] = framerate / ts_rate_decimator[tl];
618 rc->layer_pfb[i] =
619 1000.0 *
620 (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
621 (rc->layer_framerate[tl] - rc->layer_framerate[tl - 1]);
622 }
623 rc->layer_input_frames[tl] = 0;
624 rc->layer_enc_frames[tl] = 0;
625 rc->layer_encoding_bitrate[i] = 0.0;
626 rc->layer_avg_frame_size[i] = 0.0;
627 rc->layer_avg_rate_mismatch[i] = 0.0;
628 }
629 }
630 rc->window_count = 0;
631 rc->window_size = 15;
632 rc->avg_st_encoding_bitrate = 0.0;
633 rc->variance_st_encoding_bitrate = 0.0;
634}
635
636static void printout_rate_control_summary(struct RateControlMetrics *rc,
637 int frame_cnt, int ss_number_layers,
638 int ts_number_layers) {
639 int tot_num_frames = 0;
640 double perc_fluctuation = 0.0;
641 printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
642 printf("Rate control layer stats for %d layer(s):\n\n", ts_number_layers);
643 for (int sl = 0; sl < ss_number_layers; ++sl) {
644 tot_num_frames = 0;
645 for (int tl = 0; tl < ts_number_layers; ++tl) {
646 int i = sl * ts_number_layers + tl;
647 const int num_dropped =
648 tl > 0 ? rc->layer_input_frames[tl] - rc->layer_enc_frames[tl]
649 : rc->layer_input_frames[tl] - rc->layer_enc_frames[tl] - 1;
650 tot_num_frames += rc->layer_input_frames[tl];
651 rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[tl] *
652 rc->layer_encoding_bitrate[i] /
653 tot_num_frames;
654 rc->layer_avg_frame_size[i] =
655 rc->layer_avg_frame_size[i] / rc->layer_enc_frames[tl];
656 rc->layer_avg_rate_mismatch[i] =
657 100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[tl];
658 printf("For layer#: %d %d \n", sl, tl);
659 printf("Bitrate (target vs actual): %d %f\n", rc->layer_target_bitrate[i],
660 rc->layer_encoding_bitrate[i]);
661 printf("Average frame size (target vs actual): %f %f\n", rc->layer_pfb[i],
662 rc->layer_avg_frame_size[i]);
663 printf("Average rate_mismatch: %f\n", rc->layer_avg_rate_mismatch[i]);
664 printf(
665 "Number of input frames, encoded (non-key) frames, "
666 "and perc dropped frames: %d %d %f\n",
667 rc->layer_input_frames[tl], rc->layer_enc_frames[tl],
668 100.0 * num_dropped / rc->layer_input_frames[tl]);
669 printf("\n");
670 }
671 }
672 rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
673 rc->variance_st_encoding_bitrate =
674 rc->variance_st_encoding_bitrate / rc->window_count -
675 (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
676 perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
677 rc->avg_st_encoding_bitrate;
678 printf("Short-time stats, for window of %d frames:\n", rc->window_size);
679 printf("Average, rms-variance, and percent-fluct: %f %f %f\n",
680 rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
681 perc_fluctuation);
682 if (frame_cnt - 1 != tot_num_frames)
683 die("Error: Number of input frames not equal to output!\n");
684}
685
686// Layer pattern configuration.
687static void set_layer_pattern(
688 int layering_mode, int superframe_cnt, aom_svc_layer_id_t *layer_id,
689 aom_svc_ref_frame_config_t *ref_frame_config,
690 aom_svc_ref_frame_comp_pred_t *ref_frame_comp_pred, int *use_svc_control,
691 int spatial_layer_id, int is_key_frame, int ksvc_mode, int speed,
692 int *reference_updated) {
693 // Setting this flag to 1 enables simplex example of
694 // RPS (Reference Picture Selection) for 1 layer.
695 int use_rps_example = 0;
696 int i;
697 int enable_longterm_temporal_ref = 1;
698 int shift = (layering_mode == 8) ? 2 : 0;
699 int simulcast_mode = (layering_mode == 11);
700 *use_svc_control = 1;
701 layer_id->spatial_layer_id = spatial_layer_id;
702 int lag_index = 0;
703 int base_count = superframe_cnt >> 2;
704 ref_frame_comp_pred->use_comp_pred[0] = 0; // GOLDEN_LAST
705 ref_frame_comp_pred->use_comp_pred[1] = 0; // LAST2_LAST
706 ref_frame_comp_pred->use_comp_pred[2] = 0; // ALTREF_LAST
707 // Set the reference map buffer idx for the 7 references:
708 // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
709 // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
710 for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->ref_idx[i] = i;
711 for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->reference[i] = 0;
712 for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
713
714 if (ksvc_mode) {
715 // Same pattern as case 9, but the reference strucutre will be constrained
716 // below.
717 layering_mode = 9;
718 }
719 switch (layering_mode) {
720 case 0:
721 if (use_rps_example == 0) {
722 // 1-layer: update LAST on every frame, reference LAST.
723 layer_id->temporal_layer_id = 0;
724 layer_id->spatial_layer_id = 0;
725 ref_frame_config->refresh[0] = 1;
726 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
727 } else {
728 // Pattern of 2 references (ALTREF and GOLDEN) trailing
729 // LAST by 4 and 8 frames, with some switching logic to
730 // sometimes only predict from the longer-term reference
731 //(golden here). This is simple example to test RPS
732 // (reference picture selection).
733 int last_idx = 0;
734 int last_idx_refresh = 0;
735 int gld_idx = 0;
736 int alt_ref_idx = 0;
737 int lag_alt = 4;
738 int lag_gld = 8;
739 layer_id->temporal_layer_id = 0;
740 layer_id->spatial_layer_id = 0;
741 int sh = 8; // slots 0 - 7.
742 // Moving index slot for last: 0 - (sh - 1)
743 if (superframe_cnt > 1) last_idx = (superframe_cnt - 1) % sh;
744 // Moving index for refresh of last: one ahead for next frame.
745 last_idx_refresh = superframe_cnt % sh;
746 // Moving index for gld_ref, lag behind current by lag_gld
747 if (superframe_cnt > lag_gld) gld_idx = (superframe_cnt - lag_gld) % sh;
748 // Moving index for alt_ref, lag behind LAST by lag_alt frames.
749 if (superframe_cnt > lag_alt)
750 alt_ref_idx = (superframe_cnt - lag_alt) % sh;
751 // Set the ref_idx.
752 // Default all references to slot for last.
753 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
754 ref_frame_config->ref_idx[i] = last_idx;
755 // Set the ref_idx for the relevant references.
756 ref_frame_config->ref_idx[SVC_LAST_FRAME] = last_idx;
757 ref_frame_config->ref_idx[SVC_LAST2_FRAME] = last_idx_refresh;
758 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = gld_idx;
759 ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = alt_ref_idx;
760 // Refresh this slot, which will become LAST on next frame.
761 ref_frame_config->refresh[last_idx_refresh] = 1;
762 // Reference LAST, ALTREF, and GOLDEN
763 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
764 ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
765 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
766 // Switch to only GOLDEN every 300 frames.
767 if (superframe_cnt % 200 == 0 && superframe_cnt > 0) {
768 ref_frame_config->reference[SVC_LAST_FRAME] = 0;
769 ref_frame_config->reference[SVC_ALTREF_FRAME] = 0;
770 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
771 // Test if the long-term is LAST instead, this is just a renaming
772 // but its tests if encoder behaves the same, whether its
773 // LAST or GOLDEN.
774 if (superframe_cnt % 400 == 0 && superframe_cnt > 0) {
775 ref_frame_config->ref_idx[SVC_LAST_FRAME] = gld_idx;
776 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
777 ref_frame_config->reference[SVC_ALTREF_FRAME] = 0;
778 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 0;
779 }
780 }
781 }
782 break;
783 case 1:
784 // 2-temporal layer.
785 // 1 3 5
786 // 0 2 4
787 // Keep golden fixed at slot 3.
788 base_count = superframe_cnt >> 1;
789 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
790 // Cyclically refresh slots 5, 6, 7, for lag alt ref.
791 lag_index = 5;
792 if (base_count > 0) {
793 lag_index = 5 + (base_count % 3);
794 if (superframe_cnt % 2 != 0) lag_index = 5 + ((base_count + 1) % 3);
795 }
796 // Set the altref slot to lag_index.
797 ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = lag_index;
798 if (superframe_cnt % 2 == 0) {
799 layer_id->temporal_layer_id = 0;
800 // Update LAST on layer 0, reference LAST.
801 ref_frame_config->refresh[0] = 1;
802 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
803 // Refresh lag_index slot, needed for lagging golen.
804 ref_frame_config->refresh[lag_index] = 1;
805 // Refresh GOLDEN every x base layer frames.
806 if (base_count % 32 == 0) ref_frame_config->refresh[3] = 1;
807 } else {
808 layer_id->temporal_layer_id = 1;
809 // No updates on layer 1, reference LAST (TL0).
810 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
811 }
812 // Always reference golden and altref on TL0.
813 if (layer_id->temporal_layer_id == 0) {
814 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
815 ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
816 }
817 break;
818 case 2:
819 // 3-temporal layer:
820 // 1 3 5 7
821 // 2 6
822 // 0 4 8
823 if (superframe_cnt % 4 == 0) {
824 // Base layer.
825 layer_id->temporal_layer_id = 0;
826 // Update LAST on layer 0, reference LAST.
827 ref_frame_config->refresh[0] = 1;
828 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
829 } else if ((superframe_cnt - 1) % 4 == 0) {
830 layer_id->temporal_layer_id = 2;
831 // First top layer: no updates, only reference LAST (TL0).
832 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
833 } else if ((superframe_cnt - 2) % 4 == 0) {
834 layer_id->temporal_layer_id = 1;
835 // Middle layer (TL1): update LAST2, only reference LAST (TL0).
836 ref_frame_config->refresh[1] = 1;
837 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
838 } else if ((superframe_cnt - 3) % 4 == 0) {
839 layer_id->temporal_layer_id = 2;
840 // Second top layer: no updates, only reference LAST.
841 // Set buffer idx for LAST to slot 1, since that was the slot
842 // updated in previous frame. So LAST is TL1 frame.
843 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
844 ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
845 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
846 }
847 break;
848 case 3:
849 // 3 TL, same as above, except allow for predicting
850 // off 2 more references (GOLDEN and ALTREF), with
851 // GOLDEN updated periodically, and ALTREF lagging from
852 // LAST from ~4 frames. Both GOLDEN and ALTREF
853 // can only be updated on base temporal layer.
854
855 // Keep golden fixed at slot 3.
856 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
857 // Cyclically refresh slots 5, 6, 7, for lag altref.
858 lag_index = 5;
859 if (base_count > 0) {
860 lag_index = 5 + (base_count % 3);
861 if (superframe_cnt % 4 != 0) lag_index = 5 + ((base_count + 1) % 3);
862 }
863 // Set the altref slot to lag_index.
864 ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = lag_index;
865 if (superframe_cnt % 4 == 0) {
866 // Base layer.
867 layer_id->temporal_layer_id = 0;
868 // Update LAST on layer 0, reference LAST.
869 ref_frame_config->refresh[0] = 1;
870 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
871 // Refresh GOLDEN every x ~10 base layer frames.
872 if (base_count % 10 == 0) ref_frame_config->refresh[3] = 1;
873 // Refresh lag_index slot, needed for lagging altref.
874 ref_frame_config->refresh[lag_index] = 1;
875 } else if ((superframe_cnt - 1) % 4 == 0) {
876 layer_id->temporal_layer_id = 2;
877 // First top layer: no updates, only reference LAST (TL0).
878 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
879 } else if ((superframe_cnt - 2) % 4 == 0) {
880 layer_id->temporal_layer_id = 1;
881 // Middle layer (TL1): update LAST2, only reference LAST (TL0).
882 ref_frame_config->refresh[1] = 1;
883 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
884 } else if ((superframe_cnt - 3) % 4 == 0) {
885 layer_id->temporal_layer_id = 2;
886 // Second top layer: no updates, only reference LAST.
887 // Set buffer idx for LAST to slot 1, since that was the slot
888 // updated in previous frame. So LAST is TL1 frame.
889 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
890 ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
891 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
892 }
893 // Every frame can reference GOLDEN AND ALTREF.
894 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
895 ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
896 // Allow for compound prediction for LAST-ALTREF and LAST-GOLDEN.
897 if (speed >= 7) {
898 ref_frame_comp_pred->use_comp_pred[2] = 1;
899 ref_frame_comp_pred->use_comp_pred[0] = 1;
900 }
901 break;
902 case 4:
903 // 3-temporal layer: but middle layer updates GF, so 2nd TL2 will
904 // only reference GF (not LAST). Other frames only reference LAST.
905 // 1 3 5 7
906 // 2 6
907 // 0 4 8
908 if (superframe_cnt % 4 == 0) {
909 // Base layer.
910 layer_id->temporal_layer_id = 0;
911 // Update LAST on layer 0, only reference LAST.
912 ref_frame_config->refresh[0] = 1;
913 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
914 } else if ((superframe_cnt - 1) % 4 == 0) {
915 layer_id->temporal_layer_id = 2;
916 // First top layer: no updates, only reference LAST (TL0).
917 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
918 } else if ((superframe_cnt - 2) % 4 == 0) {
919 layer_id->temporal_layer_id = 1;
920 // Middle layer (TL1): update GF, only reference LAST (TL0).
921 ref_frame_config->refresh[3] = 1;
922 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
923 } else if ((superframe_cnt - 3) % 4 == 0) {
924 layer_id->temporal_layer_id = 2;
925 // Second top layer: no updates, only reference GF.
926 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
927 }
928 break;
929
930 case 5:
931 /*
932 // 2 spatial layers, 1 temporal, without temporal prediction on SL1.
933 layer_id->temporal_layer_id = 0;
934 if (layer_id->spatial_layer_id == 0) {
935 // Reference LAST, update LAST.
936 ref_frame_config->refresh[0] = 1;
937 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
938 } else if (layer_id->spatial_layer_id == 1) {
939 // Reference LAST, which is SL0, and no refresh.
940 ref_frame_config->refresh[0] = 0;
941 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
942 }
943 break;
944 */
945 // 2 spatial layers, 1 temporal.
946 layer_id->temporal_layer_id = 0;
947 if (layer_id->spatial_layer_id == 0) {
948 // Reference LAST, update LAST.
949 ref_frame_config->refresh[0] = 1;
950 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
951 ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 2;
952 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
953 } else if (layer_id->spatial_layer_id == 1) {
954 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
955 // and GOLDEN to slot 0. Update slot 1 (GOLDEN).
956 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
957 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 0;
958 ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 2;
959 ref_frame_config->refresh[1] = 1;
960 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
961 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
962 }
963 break;
964
965 case 6:
966 // 3 spatial layers, 1 temporal.
967 // Note for this case, we set the buffer idx for all references to be
968 // either LAST or GOLDEN, which are always valid references, since decoder
969 // will check if any of the 7 references is valid scale in
970 // valid_ref_frame_size().
971 layer_id->temporal_layer_id = 0;
972 if (layer_id->spatial_layer_id == 0) {
973 // Reference LAST, update LAST. Set all buffer_idx to 0.
974 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
975 ref_frame_config->ref_idx[i] = 0;
976 ref_frame_config->refresh[0] = 1;
977 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
978 } else if (layer_id->spatial_layer_id == 1) {
979 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
980 // and GOLDEN (and all other refs) to slot 0.
981 // Update slot 1 (LAST).
982 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
983 ref_frame_config->ref_idx[i] = 0;
984 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
985 ref_frame_config->refresh[1] = 1;
986 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
987 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
988 } else if (layer_id->spatial_layer_id == 2) {
989 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2
990 // and GOLDEN (and all other refs) to slot 1.
991 // Update slot 2 (LAST).
992 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
993 ref_frame_config->ref_idx[i] = 1;
994 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
995 ref_frame_config->refresh[2] = 1;
996 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
997 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
998 // For 3 spatial layer case: allow for top spatial layer to use
999 // additional temporal reference. Update every 10 frames.
1000 if (enable_longterm_temporal_ref) {
1001 ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
1002 ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
1003 if (base_count % 10 == 0)
1004 ref_frame_config->refresh[REF_FRAMES - 1] = 1;
1005 }
1006 }
1007 break;
1008 case 7:
1009 // 2 spatial and 3 temporal layer.
1010 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1011 if (superframe_cnt % 4 == 0) {
1012 // Base temporal layer
1013 layer_id->temporal_layer_id = 0;
1014 if (layer_id->spatial_layer_id == 0) {
1015 // Reference LAST, update LAST
1016 // Set all buffer_idx to 0
1017 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1018 ref_frame_config->ref_idx[i] = 0;
1019 ref_frame_config->refresh[0] = 1;
1020 } else if (layer_id->spatial_layer_id == 1) {
1021 // Reference LAST and GOLDEN.
1022 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1023 ref_frame_config->ref_idx[i] = 0;
1024 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1025 ref_frame_config->refresh[1] = 1;
1026 }
1027 } else if ((superframe_cnt - 1) % 4 == 0) {
1028 // First top temporal enhancement layer.
1029 layer_id->temporal_layer_id = 2;
1030 if (layer_id->spatial_layer_id == 0) {
1031 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1032 ref_frame_config->ref_idx[i] = 0;
1033 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1034 ref_frame_config->refresh[3] = 1;
1035 } else if (layer_id->spatial_layer_id == 1) {
1036 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
1037 // GOLDEN (and all other refs) to slot 3.
1038 // No update.
1039 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1040 ref_frame_config->ref_idx[i] = 3;
1041 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1042 }
1043 } else if ((superframe_cnt - 2) % 4 == 0) {
1044 // Middle temporal enhancement layer.
1045 layer_id->temporal_layer_id = 1;
1046 if (layer_id->spatial_layer_id == 0) {
1047 // Reference LAST.
1048 // Set all buffer_idx to 0.
1049 // Set GOLDEN to slot 5 and update slot 5.
1050 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1051 ref_frame_config->ref_idx[i] = 0;
1052 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
1053 ref_frame_config->refresh[5 - shift] = 1;
1054 } else if (layer_id->spatial_layer_id == 1) {
1055 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
1056 // GOLDEN (and all other refs) to slot 5.
1057 // Set LAST3 to slot 6 and update slot 6.
1058 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1059 ref_frame_config->ref_idx[i] = 5 - shift;
1060 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1061 ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
1062 ref_frame_config->refresh[6 - shift] = 1;
1063 }
1064 } else if ((superframe_cnt - 3) % 4 == 0) {
1065 // Second top temporal enhancement layer.
1066 layer_id->temporal_layer_id = 2;
1067 if (layer_id->spatial_layer_id == 0) {
1068 // Set LAST to slot 5 and reference LAST.
1069 // Set GOLDEN to slot 3 and update slot 3.
1070 // Set all other buffer_idx to 0.
1071 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1072 ref_frame_config->ref_idx[i] = 0;
1073 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
1074 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1075 ref_frame_config->refresh[3] = 1;
1076 } else if (layer_id->spatial_layer_id == 1) {
1077 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
1078 // GOLDEN to slot 3. No update.
1079 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1080 ref_frame_config->ref_idx[i] = 0;
1081 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
1082 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1083 }
1084 }
1085 break;
1086 case 8:
1087 // 3 spatial and 3 temporal layer.
1088 // Same as case 9 but overalap in the buffer slot updates.
1089 // (shift = 2). The slots 3 and 4 updated by first TL2 are
1090 // reused for update in TL1 superframe.
1091 // Note for this case, frame order hint must be disabled for
1092 // lower resolutios (operating points > 0) to be decoedable.
1093 case 9:
1094 // 3 spatial and 3 temporal layer.
1095 // No overlap in buffer updates between TL2 and TL1.
1096 // TL2 updates slot 3 and 4, TL1 updates 5, 6, 7.
1097 // Set the references via the svc_ref_frame_config control.
1098 // Always reference LAST.
1099 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1100 if (superframe_cnt % 4 == 0) {
1101 // Base temporal layer.
1102 layer_id->temporal_layer_id = 0;
1103 if (layer_id->spatial_layer_id == 0) {
1104 // Reference LAST, update LAST.
1105 // Set all buffer_idx to 0.
1106 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1107 ref_frame_config->ref_idx[i] = 0;
1108 ref_frame_config->refresh[0] = 1;
1109 } else if (layer_id->spatial_layer_id == 1) {
1110 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
1111 // GOLDEN (and all other refs) to slot 0.
1112 // Update slot 1 (LAST).
1113 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1114 ref_frame_config->ref_idx[i] = 0;
1115 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1116 ref_frame_config->refresh[1] = 1;
1117 } else if (layer_id->spatial_layer_id == 2) {
1118 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
1119 // GOLDEN (and all other refs) to slot 1.
1120 // Update slot 2 (LAST).
1121 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1122 ref_frame_config->ref_idx[i] = 1;
1123 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1124 ref_frame_config->refresh[2] = 1;
1125 }
1126 } else if ((superframe_cnt - 1) % 4 == 0) {
1127 // First top temporal enhancement layer.
1128 layer_id->temporal_layer_id = 2;
1129 if (layer_id->spatial_layer_id == 0) {
1130 // Reference LAST (slot 0).
1131 // Set GOLDEN to slot 3 and update slot 3.
1132 // Set all other buffer_idx to slot 0.
1133 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1134 ref_frame_config->ref_idx[i] = 0;
1135 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1136 ref_frame_config->refresh[3] = 1;
1137 } else if (layer_id->spatial_layer_id == 1) {
1138 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
1139 // GOLDEN (and all other refs) to slot 3.
1140 // Set LAST2 to slot 4 and Update slot 4.
1141 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1142 ref_frame_config->ref_idx[i] = 3;
1143 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1144 ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
1145 ref_frame_config->refresh[4] = 1;
1146 } else if (layer_id->spatial_layer_id == 2) {
1147 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
1148 // GOLDEN (and all other refs) to slot 4.
1149 // No update.
1150 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1151 ref_frame_config->ref_idx[i] = 4;
1152 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1153 }
1154 } else if ((superframe_cnt - 2) % 4 == 0) {
1155 // Middle temporal enhancement layer.
1156 layer_id->temporal_layer_id = 1;
1157 if (layer_id->spatial_layer_id == 0) {
1158 // Reference LAST.
1159 // Set all buffer_idx to 0.
1160 // Set GOLDEN to slot 5 and update slot 5.
1161 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1162 ref_frame_config->ref_idx[i] = 0;
1163 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
1164 ref_frame_config->refresh[5 - shift] = 1;
1165 } else if (layer_id->spatial_layer_id == 1) {
1166 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
1167 // GOLDEN (and all other refs) to slot 5.
1168 // Set LAST3 to slot 6 and update slot 6.
1169 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1170 ref_frame_config->ref_idx[i] = 5 - shift;
1171 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1172 ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
1173 ref_frame_config->refresh[6 - shift] = 1;
1174 } else if (layer_id->spatial_layer_id == 2) {
1175 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
1176 // GOLDEN (and all other refs) to slot 6.
1177 // Set LAST3 to slot 7 and update slot 7.
1178 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1179 ref_frame_config->ref_idx[i] = 6 - shift;
1180 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1181 ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 7 - shift;
1182 ref_frame_config->refresh[7 - shift] = 1;
1183 }
1184 } else if ((superframe_cnt - 3) % 4 == 0) {
1185 // Second top temporal enhancement layer.
1186 layer_id->temporal_layer_id = 2;
1187 if (layer_id->spatial_layer_id == 0) {
1188 // Set LAST to slot 5 and reference LAST.
1189 // Set GOLDEN to slot 3 and update slot 3.
1190 // Set all other buffer_idx to 0.
1191 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1192 ref_frame_config->ref_idx[i] = 0;
1193 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
1194 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1195 ref_frame_config->refresh[3] = 1;
1196 } else if (layer_id->spatial_layer_id == 1) {
1197 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
1198 // GOLDEN to slot 3. Set LAST2 to slot 4 and update slot 4.
1199 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1200 ref_frame_config->ref_idx[i] = 0;
1201 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
1202 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1203 ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
1204 ref_frame_config->refresh[4] = 1;
1205 } else if (layer_id->spatial_layer_id == 2) {
1206 // Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 7,
1207 // GOLDEN to slot 4. No update.
1208 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1209 ref_frame_config->ref_idx[i] = 0;
1210 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 7 - shift;
1211 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 4;
1212 }
1213 }
1214 break;
1215 case 11:
1216 // Simulcast mode for 3 spatial and 3 temporal layers.
1217 // No inter-layer predicton, only prediction is temporal and single
1218 // reference (LAST).
1219 // No overlap in buffer slots between spatial layers. So for example,
1220 // SL0 only uses slots 0 and 1.
1221 // SL1 only uses slots 2 and 3.
1222 // SL2 only uses slots 4 and 5.
1223 // All 7 references for each inter-frame must only access buffer slots
1224 // for that spatial layer.
1225 // On key (super)frames: SL1 and SL2 must have no references set
1226 // and must refresh all the slots for that layer only (so 2 and 3
1227 // for SL1, 4 and 5 for SL2). The base SL0 will be labelled internally
1228 // as a Key frame (refresh all slots). SL1/SL2 will be labelled
1229 // internally as Intra-only frames that allow that stream to be decoded.
1230 // These conditions will allow for each spatial stream to be
1231 // independently decodeable.
1232
1233 // Initialize all references to 0 (don't use reference).
1234 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1235 ref_frame_config->reference[i] = 0;
1236 // Initialize as no refresh/update for all slots.
1237 for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
1238 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1239 ref_frame_config->ref_idx[i] = 0;
1240
1241 if (is_key_frame) {
1242 if (layer_id->spatial_layer_id == 0) {
1243 // Assign LAST/GOLDEN to slot 0/1.
1244 // Refesh slots 0 and 1 for SL0.
1245 // SL0: this will get set to KEY frame internally.
1246 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
1247 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 1;
1248 ref_frame_config->refresh[0] = 1;
1249 ref_frame_config->refresh[1] = 1;
1250 } else if (layer_id->spatial_layer_id == 1) {
1251 // Assign LAST/GOLDEN to slot 2/3.
1252 // Refesh slots 2 and 3 for SL1.
1253 // This will get set to Intra-only frame internally.
1254 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1255 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
1256 ref_frame_config->refresh[2] = 1;
1257 ref_frame_config->refresh[3] = 1;
1258 } else if (layer_id->spatial_layer_id == 2) {
1259 // Assign LAST/GOLDEN to slot 4/5.
1260 // Refresh slots 4 and 5 for SL2.
1261 // This will get set to Intra-only frame internally.
1262 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
1263 ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5;
1264 ref_frame_config->refresh[4] = 1;
1265 ref_frame_config->refresh[5] = 1;
1266 }
1267 } else if (superframe_cnt % 4 == 0) {
1268 // Base temporal layer: TL0
1269 layer_id->temporal_layer_id = 0;
1270 if (layer_id->spatial_layer_id == 0) { // SL0
1271 // Reference LAST. Assign all references to either slot
1272 // 0 or 1. Here we assign LAST to slot 0, all others to 1.
1273 // Update slot 0 (LAST).
1274 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1275 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1276 ref_frame_config->ref_idx[i] = 1;
1277 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
1278 ref_frame_config->refresh[0] = 1;
1279 } else if (layer_id->spatial_layer_id == 1) { // SL1
1280 // Reference LAST. Assign all references to either slot
1281 // 2 or 3. Here we assign LAST to slot 2, all others to 3.
1282 // Update slot 2 (LAST).
1283 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1284 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1285 ref_frame_config->ref_idx[i] = 3;
1286 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1287 ref_frame_config->refresh[2] = 1;
1288 } else if (layer_id->spatial_layer_id == 2) { // SL2
1289 // Reference LAST. Assign all references to either slot
1290 // 4 or 5. Here we assign LAST to slot 4, all others to 5.
1291 // Update slot 4 (LAST).
1292 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1293 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1294 ref_frame_config->ref_idx[i] = 5;
1295 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
1296 ref_frame_config->refresh[4] = 1;
1297 }
1298 } else if ((superframe_cnt - 1) % 4 == 0) {
1299 // First top temporal enhancement layer: TL2
1300 layer_id->temporal_layer_id = 2;
1301 if (layer_id->spatial_layer_id == 0) { // SL0
1302 // Reference LAST (slot 0). Assign other references to slot 1.
1303 // No update/refresh on any slots.
1304 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1305 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1306 ref_frame_config->ref_idx[i] = 1;
1307 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
1308 } else if (layer_id->spatial_layer_id == 1) { // SL1
1309 // Reference LAST (slot 2). Assign other references to slot 3.
1310 // No update/refresh on any slots.
1311 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1312 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1313 ref_frame_config->ref_idx[i] = 3;
1314 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1315 } else if (layer_id->spatial_layer_id == 2) { // SL2
1316 // Reference LAST (slot 4). Assign other references to slot 4.
1317 // No update/refresh on any slots.
1318 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1319 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1320 ref_frame_config->ref_idx[i] = 5;
1321 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
1322 }
1323 } else if ((superframe_cnt - 2) % 4 == 0) {
1324 // Middle temporal enhancement layer: TL1
1325 layer_id->temporal_layer_id = 1;
1326 if (layer_id->spatial_layer_id == 0) { // SL0
1327 // Reference LAST (slot 0).
1328 // Set GOLDEN to slot 1 and update slot 1.
1329 // This will be used as reference for next TL2.
1330 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1331 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1332 ref_frame_config->ref_idx[i] = 1;
1333 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
1334 ref_frame_config->refresh[1] = 1;
1335 } else if (layer_id->spatial_layer_id == 1) { // SL1
1336 // Reference LAST (slot 2).
1337 // Set GOLDEN to slot 3 and update slot 3.
1338 // This will be used as reference for next TL2.
1339 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1340 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1341 ref_frame_config->ref_idx[i] = 3;
1342 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
1343 ref_frame_config->refresh[3] = 1;
1344 } else if (layer_id->spatial_layer_id == 2) { // SL2
1345 // Reference LAST (slot 4).
1346 // Set GOLDEN to slot 5 and update slot 5.
1347 // This will be used as reference for next TL2.
1348 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1349 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1350 ref_frame_config->ref_idx[i] = 5;
1351 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
1352 ref_frame_config->refresh[5] = 1;
1353 }
1354 } else if ((superframe_cnt - 3) % 4 == 0) {
1355 // Second top temporal enhancement layer: TL2
1356 layer_id->temporal_layer_id = 2;
1357 if (layer_id->spatial_layer_id == 0) { // SL0
1358 // Reference LAST (slot 1). Assign other references to slot 0.
1359 // No update/refresh on any slots.
1360 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1361 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1362 ref_frame_config->ref_idx[i] = 0;
1363 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
1364 } else if (layer_id->spatial_layer_id == 1) { // SL1
1365 // Reference LAST (slot 3). Assign other references to slot 2.
1366 // No update/refresh on any slots.
1367 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1368 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1369 ref_frame_config->ref_idx[i] = 2;
1370 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 3;
1371 } else if (layer_id->spatial_layer_id == 2) { // SL2
1372 // Reference LAST (slot 5). Assign other references to slot 4.
1373 // No update/refresh on any slots.
1374 ref_frame_config->reference[SVC_LAST_FRAME] = 1;
1375 for (i = 0; i < INTER_REFS_PER_FRAME; i++)
1376 ref_frame_config->ref_idx[i] = 4;
1377 ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5;
1378 }
1379 }
1380 if (!simulcast_mode && layer_id->spatial_layer_id > 0) {
1381 // Always reference GOLDEN (inter-layer prediction).
1382 ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
1383 if (ksvc_mode) {
1384 // KSVC: only keep the inter-layer reference (GOLDEN) for
1385 // superframes whose base is key.
1386 if (!is_key_frame) ref_frame_config->reference[SVC_GOLDEN_FRAME] = 0;
1387 }
1388 if (is_key_frame && layer_id->spatial_layer_id > 1) {
1389 // On superframes whose base is key: remove LAST to avoid prediction
1390 // off layer two levels below.
1391 ref_frame_config->reference[SVC_LAST_FRAME] = 0;
1392 }
1393 }
1394 // For 3 spatial layer case 8 (where there is free buffer slot):
1395 // allow for top spatial layer to use additional temporal reference.
1396 // Additional reference is only updated on base temporal layer, every
1397 // 10 TL0 frames here.
1398 if (!simulcast_mode && enable_longterm_temporal_ref &&
1399 layer_id->spatial_layer_id == 2 && layering_mode == 8) {
1400 ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
1401 if (!is_key_frame) ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
1402 if (base_count % 10 == 0 && layer_id->temporal_layer_id == 0)
1403 ref_frame_config->refresh[REF_FRAMES - 1] = 1;
1404 }
1405 break;
1406 default: assert(0); die("Error: Unsupported temporal layering mode!\n");
1407 }
1408 for (i = 0; i < REF_FRAMES; i++) {
1409 if (ref_frame_config->refresh[i] == 1) {
1410 *reference_updated = 1;
1411 break;
1412 }
1413 }
1414}
1415
1416static void write_literal(struct aom_write_bit_buffer *wb, uint32_t data,
1417 uint8_t bits, uint32_t offset = 0) {
1418 if (bits > 32) {
1419 die("Invalid bits value %d > 32\n", bits);
1420 }
1421 const uint32_t max = static_cast<uint32_t>(((uint64_t)1 << bits) - 1);
1422 if (data < offset || (data - offset) > max) {
1423 die("Invalid data, value %u out of range [%u, %" PRIu64 "]\n", data, offset,
1424 (uint64_t)max + offset);
1425 }
1426 aom_wb_write_unsigned_literal(wb, data - offset, bits);
1427}
1428
1429static void write_depth_representation_element(
1430 struct aom_write_bit_buffer *buffer,
1431 const std::pair<libaom_examples::DepthRepresentationElement, bool>
1432 &element) {
1433 if (!element.second) {
1434 return;
1435 }
1436 write_literal(buffer, element.first.sign_flag, 1);
1437 write_literal(buffer, element.first.exponent, 7);
1438 if (element.first.mantissa_len == 0 || element.first.mantissa_len > 32) {
1439 die("Invalid mantissan_len %d\n", element.first.mantissa_len);
1440 }
1441 write_literal(buffer, element.first.mantissa_len - 1, 5);
1442 write_literal(buffer, element.first.mantissa, element.first.mantissa_len);
1443}
1444
1445static void write_color_properties(
1446 struct aom_write_bit_buffer *buffer,
1447 const std::pair<libaom_examples::ColorProperties, bool> &color_properties) {
1448 write_literal(buffer, color_properties.second, 1);
1449 if (color_properties.second) {
1450 write_literal(buffer, color_properties.first.color_range, 1);
1451 write_literal(buffer, color_properties.first.color_primaries, 8);
1452 write_literal(buffer, color_properties.first.transfer_characteristics, 8);
1453 write_literal(buffer, color_properties.first.matrix_coefficients, 8);
1454 } else {
1455 write_literal(buffer, 0, 1); // reserved_1bit
1456 }
1457}
1458
1459static void write_alpha_information(
1460 struct aom_write_bit_buffer *buffer,
1461 const libaom_examples::AlphaInformation &alpha_info) {
1462 write_literal(buffer, alpha_info.alpha_use_idc, 2);
1463 write_literal(buffer, alpha_info.alpha_simple_flag, 1);
1464 if (!alpha_info.alpha_simple_flag) {
1465 write_literal(buffer, alpha_info.alpha_bit_depth, 3, /*offset=*/8);
1466 write_literal(buffer, alpha_info.alpha_clip_idc, 2);
1467 write_literal(buffer, alpha_info.alpha_incr_flag, 1);
1468 write_literal(buffer, alpha_info.alpha_transparent_value,
1469 alpha_info.alpha_bit_depth + 1);
1470 write_literal(buffer, alpha_info.alpha_opaque_value,
1471 alpha_info.alpha_bit_depth + 1);
1472 if (buffer->bit_offset % 8 != 0) {
1473 // ai_byte_alignment_bits
1474 write_literal(buffer, 0, 8 - (buffer->bit_offset % 8));
1475 }
1476 assert(buffer->bit_offset % 8 == 0);
1477
1478 write_literal(buffer, 0, 6); // ai_reserved_6bits
1479 write_color_properties(buffer, alpha_info.alpha_color_description);
1480 } else {
1481 write_literal(buffer, 0, 5); // ai_reserved_5bits
1482 }
1483}
1484
1485static void write_depth_information(
1486 struct aom_write_bit_buffer *buffer,
1487 const libaom_examples::DepthInformation &depth_info) {
1488 write_literal(buffer, depth_info.z_near.second, 1);
1489 write_literal(buffer, depth_info.z_far.second, 1);
1490 write_literal(buffer, depth_info.d_min.second, 1);
1491 write_literal(buffer, depth_info.d_max.second, 1);
1492 write_literal(buffer, depth_info.depth_representation_type, 4);
1493 if (depth_info.d_min.second || depth_info.d_max.second) {
1494 write_literal(buffer, depth_info.disparity_ref_view_id, 2);
1495 }
1496 write_depth_representation_element(buffer, depth_info.z_near);
1497 write_depth_representation_element(buffer, depth_info.z_far);
1498 write_depth_representation_element(buffer, depth_info.d_min);
1499 write_depth_representation_element(buffer, depth_info.d_max);
1500 if (buffer->bit_offset % 8 != 0) {
1501 write_literal(buffer, 0, 8 - (buffer->bit_offset % 8));
1502 }
1503}
1504
1505static void add_multilayer_metadata(
1506 aom_image_t *frame, const libaom_examples::MultilayerMetadata &multilayer,
1507 int frame_idx, int spatial_id) {
1508 // Large enough buffer for the multilayer metadata.
1509 // Each layer's metadata is less than 100 bytes and there are at most 4
1510 // layers.
1511 std::vector<uint8_t> data(1024);
1512 struct aom_write_bit_buffer buffer = { data.data(), 0 };
1513
1514 write_literal(&buffer, multilayer.use_case, 6);
1515 if (multilayer.layers.empty()) {
1516 die("Invalid multilayer metadata, no layers found\n");
1517 } else if (multilayer.layers.size() > MAX_NUM_SPATIAL_LAYERS) {
1518 die("Invalid multilayer metadata, too many layers (max is %d)\n",
1519 MAX_NUM_SPATIAL_LAYERS);
1520 }
1521 write_literal(&buffer, (int)multilayer.layers.size() - 1, 2);
1522 assert(buffer.bit_offset % 8 == 0);
1523 for (size_t i = 0; i < multilayer.layers.size(); ++i) {
1524 const libaom_examples::LayerMetadata &layer = multilayer.layers[i];
1525 // Alpha info with segmentation with labels can be up to about 66k bytes,
1526 // which requires 3 bytes to encode in leb128.
1527 const int bytes_reserved_for_size = 3;
1528 // Placeholder for layer_metadata_size which will be written later.
1529 write_literal(&buffer, 0, bytes_reserved_for_size * 8);
1530 const uint32_t metadata_start = buffer.bit_offset;
1531 write_literal(&buffer, (int)i, 2); // ml_spatial_id
1532 write_literal(&buffer, layer.layer_type, 5);
1533 write_literal(&buffer, layer.luma_plane_only_flag, 1);
1534 write_literal(&buffer, layer.layer_view_type, 3);
1535 write_literal(&buffer, layer.group_id, 2);
1536 write_literal(&buffer, layer.layer_dependency_idc, 3);
1537 write_literal(&buffer, layer.layer_metadata_scope, 2);
1538 write_literal(&buffer, 0, 4); // ml_reserved_4bits
1539
1540 if (i > 0) {
1541 write_color_properties(&buffer, layer.layer_color_description);
1542 } else {
1543 write_literal(&buffer, 0, 2); // ml_reserved_2bits
1544 }
1545 assert(buffer.bit_offset % 8 == 0);
1546
1547 if (layer.layer_type == libaom_examples::MULTILAYER_LAYER_TYPE_ALPHA &&
1548 layer.layer_metadata_scope >= libaom_examples::SCOPE_GLOBAL) {
1549 write_alpha_information(&buffer, layer.alpha);
1550 assert(buffer.bit_offset % 8 == 0);
1551 } else if (layer.layer_type ==
1552 libaom_examples::MULTILAYER_LAYER_TYPE_DEPTH &&
1553 layer.layer_metadata_scope >= libaom_examples::SCOPE_GLOBAL) {
1554 write_depth_information(&buffer, layer.depth);
1555 assert(buffer.bit_offset % 8 == 0);
1556 }
1557
1558 assert(buffer.bit_offset % 8 == 0);
1559
1560 const int metadata_size_bytes = (buffer.bit_offset - metadata_start) / 8;
1561 const uint8_t size_pos = metadata_start / 8 - bytes_reserved_for_size;
1562 size_t coded_size;
1563 if (aom_uleb_encode_fixed_size(metadata_size_bytes, bytes_reserved_for_size,
1564 bytes_reserved_for_size,
1565 &buffer.bit_buffer[size_pos], &coded_size)) {
1566 // Need to increase bytes_reserved_for_size in the code above.
1567 die("Error: Failed to write metadata size\n");
1568 }
1569 }
1570 assert(buffer.bit_offset % 8 == 0);
1571 if (aom_img_add_metadata(frame, 33 /*METADATA_TYPE_MULTILAYER*/,
1572 buffer.bit_buffer, buffer.bit_offset / 8,
1574 die("Error: Failed to add metadata\n");
1575 }
1576
1577 if ((int)multilayer.layers.size() > spatial_id) {
1578 const libaom_examples::LayerMetadata &layer = multilayer.layers[spatial_id];
1579 for (const libaom_examples::FrameLocalMetadata &local_metadata :
1580 layer.local_metadata) {
1581 if (local_metadata.frame_idx == frame_idx) {
1582 if (layer.layer_type == libaom_examples::MULTILAYER_LAYER_TYPE_ALPHA) {
1583 buffer = { data.data(), 0 };
1584 write_alpha_information(&buffer, local_metadata.alpha);
1585 if (aom_img_add_metadata(frame,
1586 34 /*METADATA_TYPE_ALPHA_INFORMATION*/,
1587 buffer.bit_buffer, buffer.bit_offset / 8,
1589 die("Error: Failed to add metadata\n");
1590 }
1591 } else if (layer.layer_type ==
1592 libaom_examples::MULTILAYER_LAYER_TYPE_DEPTH) {
1593 buffer = { data.data(), 0 };
1594 write_depth_information(&buffer, local_metadata.depth);
1595 if (aom_img_add_metadata(frame,
1596 35 /*METADATA_TYPE_DEPTH_INFORMATION*/,
1597 buffer.bit_buffer, buffer.bit_offset / 8,
1599 die("Error: Failed to add metadata\n");
1600 }
1601 }
1602 break;
1603 }
1604 }
1605 }
1606}
1607
1608#if CONFIG_AV1_DECODER
1609// Returns whether there is a mismatch between the encoder's new frame and the
1610// decoder's new frame.
1611static int test_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
1612 const int frames_out) {
1613 aom_image_t enc_img, dec_img;
1614 int mismatch = 0;
1615
1616 /* Get the internal new frame */
1619
1620#if CONFIG_AV1_HIGHBITDEPTH
1621 if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
1622 (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
1623 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1624 aom_image_t enc_hbd_img;
1626 &enc_hbd_img,
1627 static_cast<aom_img_fmt_t>(enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH),
1628 enc_img.d_w, enc_img.d_h, 16);
1629 aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
1630 enc_img = enc_hbd_img;
1631 }
1632 if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1633 aom_image_t dec_hbd_img;
1635 &dec_hbd_img,
1636 static_cast<aom_img_fmt_t>(dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH),
1637 dec_img.d_w, dec_img.d_h, 16);
1638 aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
1639 dec_img = dec_hbd_img;
1640 }
1641 }
1642#endif
1643
1644 if (!aom_compare_img(&enc_img, &dec_img)) {
1645 int y[4], u[4], v[4];
1646#if CONFIG_AV1_HIGHBITDEPTH
1647 if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
1648 aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
1649 } else {
1650 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1651 }
1652#else
1653 aom_find_mismatch(&enc_img, &dec_img, y, u, v);
1654#endif
1655 fprintf(stderr,
1656 "Encode/decode mismatch on frame %d at"
1657 " Y[%d, %d] {%d/%d},"
1658 " U[%d, %d] {%d/%d},"
1659 " V[%d, %d] {%d/%d}\n",
1660 frames_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0],
1661 v[1], v[2], v[3]);
1662 mismatch = 1;
1663 }
1664
1665 aom_img_free(&enc_img);
1666 aom_img_free(&dec_img);
1667 return mismatch;
1668}
1669#endif // CONFIG_AV1_DECODER
1670
1671struct psnr_stats {
1672 // The second element of these arrays is reserved for high bitdepth.
1673 uint64_t psnr_sse_total[2];
1674 uint64_t psnr_samples_total[2];
1675 double psnr_totals[2][4];
1676 int psnr_count[2];
1677};
1678
1679static void show_psnr(struct psnr_stats *psnr_stream, double peak) {
1680 double ovpsnr;
1681
1682 if (!psnr_stream->psnr_count[0]) return;
1683
1684 fprintf(stderr, "\nPSNR (Overall/Avg/Y/U/V)");
1685 ovpsnr = sse_to_psnr((double)psnr_stream->psnr_samples_total[0], peak,
1686 (double)psnr_stream->psnr_sse_total[0]);
1687 fprintf(stderr, " %.3f", ovpsnr);
1688
1689 for (int i = 0; i < 4; i++) {
1690 fprintf(stderr, " %.3f",
1691 psnr_stream->psnr_totals[0][i] / psnr_stream->psnr_count[0]);
1692 }
1693 fprintf(stderr, "\n");
1694}
1695
1696static aom::AV1RateControlRtcConfig create_rtc_rc_config(
1697 const aom_codec_enc_cfg_t &cfg, const AppInput &app_input) {
1698 aom::AV1RateControlRtcConfig rc_cfg;
1699 rc_cfg.width = cfg.g_w;
1700 rc_cfg.height = cfg.g_h;
1701 rc_cfg.max_quantizer = cfg.rc_max_quantizer;
1702 rc_cfg.min_quantizer = cfg.rc_min_quantizer;
1703 rc_cfg.target_bandwidth = cfg.rc_target_bitrate;
1704 rc_cfg.buf_initial_sz = cfg.rc_buf_initial_sz;
1705 rc_cfg.buf_optimal_sz = cfg.rc_buf_optimal_sz;
1706 rc_cfg.buf_sz = cfg.rc_buf_sz;
1707 rc_cfg.overshoot_pct = cfg.rc_overshoot_pct;
1708 rc_cfg.undershoot_pct = cfg.rc_undershoot_pct;
1709 // This is hardcoded as AOME_SET_MAX_INTRA_BITRATE_PCT
1710 rc_cfg.max_intra_bitrate_pct = 300;
1711 rc_cfg.framerate = cfg.g_timebase.den;
1712 // TODO(jianj): Add suppor for SVC.
1713 rc_cfg.ss_number_layers = 1;
1714 rc_cfg.ts_number_layers = 1;
1715 rc_cfg.scaling_factor_num[0] = 1;
1716 rc_cfg.scaling_factor_den[0] = 1;
1717 rc_cfg.layer_target_bitrate[0] = static_cast<int>(rc_cfg.target_bandwidth);
1718 rc_cfg.max_quantizers[0] = rc_cfg.max_quantizer;
1719 rc_cfg.min_quantizers[0] = rc_cfg.min_quantizer;
1720 rc_cfg.aq_mode = app_input.aq_mode;
1721
1722 return rc_cfg;
1723}
1724
1725static int qindex_to_quantizer(int qindex) {
1726 // Table that converts 0-63 range Q values passed in outside to the 0-255
1727 // range Qindex used internally.
1728 static const int quantizer_to_qindex[] = {
1729 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48,
1730 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100,
1731 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
1732 156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
1733 208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
1734 };
1735 for (int quantizer = 0; quantizer < 64; ++quantizer)
1736 if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
1737
1738 return 63;
1739}
1740
1741static void set_active_map(const aom_codec_enc_cfg_t *cfg,
1742 aom_codec_ctx_t *codec, int frame_cnt) {
1743 aom_active_map_t map = { 0, 0, 0 };
1744
1745 map.rows = (cfg->g_h + 15) / 16;
1746 map.cols = (cfg->g_w + 15) / 16;
1747
1748 map.active_map = (uint8_t *)malloc(map.rows * map.cols);
1749 if (!map.active_map) die("Failed to allocate active map");
1750
1751 // Example map for testing.
1752 for (unsigned int i = 0; i < map.rows; ++i) {
1753 for (unsigned int j = 0; j < map.cols; ++j) {
1754 int index = map.cols * i + j;
1755 map.active_map[index] = 1;
1756 if (frame_cnt < 300) {
1757 if (i < map.rows / 2 && j < map.cols / 2) map.active_map[index] = 0;
1758 } else if (frame_cnt >= 300) {
1759 if (i < map.rows / 2 && j >= map.cols / 2) map.active_map[index] = 0;
1760 }
1761 }
1762 }
1763
1764 if (aom_codec_control(codec, AOME_SET_ACTIVEMAP, &map))
1765 die_codec(codec, "Failed to set active map");
1766
1767 free(map.active_map);
1768}
1769
1770int main(int argc, const char **argv) {
1771 AppInput app_input;
1772 AvxVideoWriter *outfile[AOM_MAX_LAYERS] = { NULL };
1773 FILE *obu_files[AOM_MAX_LAYERS] = { NULL };
1774 AvxVideoWriter *total_layer_file = NULL;
1775 FILE *total_layer_obu_file = NULL;
1777 int frame_cnt = 0;
1778 aom_image_t raw;
1779 int frame_avail;
1780 int got_data = 0;
1781 int flags = 0;
1782 int i;
1783 int pts = 0; // PTS starts at 0.
1784 int frame_duration = 1; // 1 timebase tick per frame.
1785 aom_svc_layer_id_t layer_id;
1786 aom_svc_params_t svc_params;
1787 aom_svc_ref_frame_config_t ref_frame_config;
1788 aom_svc_ref_frame_comp_pred_t ref_frame_comp_pred;
1789
1790#if CONFIG_INTERNAL_STATS
1791 FILE *stats_file = fopen("opsnr.stt", "a");
1792 if (stats_file == NULL) {
1793 die("Cannot open opsnr.stt\n");
1794 }
1795#endif
1796#if CONFIG_AV1_DECODER
1797 aom_codec_ctx_t decoder;
1798#endif
1799
1800 struct RateControlMetrics rc;
1801 int64_t cx_time = 0;
1802 int64_t cx_time_layer[AOM_MAX_LAYERS]; // max number of layers.
1803 int frame_cnt_layer[AOM_MAX_LAYERS];
1804 double sum_bitrate = 0.0;
1805 double sum_bitrate2 = 0.0;
1806 double framerate = 30.0;
1807 int use_svc_control = 1;
1808 int set_err_resil_frame = 0;
1809 int test_changing_bitrate = 0;
1810 zero(rc.layer_target_bitrate);
1811 memset(&layer_id, 0, sizeof(aom_svc_layer_id_t));
1812 memset(&app_input, 0, sizeof(AppInput));
1813 memset(&svc_params, 0, sizeof(svc_params));
1814
1815 // Flag to test dynamic scaling of source frames for single
1816 // spatial stream, using the scaling_mode control.
1817 const int test_dynamic_scaling_single_layer = 0;
1818
1819 // Flag to test setting speed per layer.
1820 const int test_speed_per_layer = 0;
1821
1822 // Flag for testing active maps.
1823 const int test_active_maps = 0;
1824
1825 /* Setup default input stream settings */
1826 for (i = 0; i < MAX_NUM_SPATIAL_LAYERS; ++i) {
1827 app_input.input_ctx[i].framerate.numerator = 30;
1828 app_input.input_ctx[i].framerate.denominator = 1;
1829 app_input.input_ctx[i].only_i420 = 0;
1830 app_input.input_ctx[i].bit_depth = AOM_BITS_8;
1831 }
1832 app_input.speed = 7;
1833 exec_name = argv[0];
1834
1835 // start with default encoder configuration
1838 if (res != AOM_CODEC_OK) {
1839 die("Failed to get config: %s\n", aom_codec_err_to_string(res));
1840 }
1841
1842 // Real time parameters.
1844
1845 cfg.rc_end_usage = AOM_CBR;
1846 cfg.rc_min_quantizer = 2;
1847 cfg.rc_max_quantizer = 52;
1848 cfg.rc_undershoot_pct = 50;
1849 cfg.rc_overshoot_pct = 50;
1850 cfg.rc_buf_initial_sz = 600;
1851 cfg.rc_buf_optimal_sz = 600;
1852 cfg.rc_buf_sz = 1000;
1853 cfg.rc_resize_mode = 0; // Set to RESIZE_DYNAMIC for dynamic resize.
1854 cfg.g_lag_in_frames = 0;
1855 cfg.kf_mode = AOM_KF_AUTO;
1856 cfg.g_w = 0; // Force user to specify width and height for raw input.
1857 cfg.g_h = 0;
1858
1859 parse_command_line(argc, argv, &app_input, &svc_params, &cfg);
1860
1861 int ts_number_layers = svc_params.number_temporal_layers;
1862 int ss_number_layers = svc_params.number_spatial_layers;
1863
1864 unsigned int width = cfg.g_w;
1865 unsigned int height = cfg.g_h;
1866
1867 if (app_input.layering_mode >= 0) {
1868 if (ts_number_layers !=
1869 mode_to_num_temporal_layers[app_input.layering_mode] ||
1870 ss_number_layers !=
1871 mode_to_num_spatial_layers[app_input.layering_mode]) {
1872 die("Number of layers doesn't match layering mode.");
1873 }
1874 }
1875
1876 bool has_non_y4m_input = false;
1877 for (i = 0; i < AOM_MAX_LAYERS; ++i) {
1878 if (app_input.input_ctx[i].file_type != FILE_TYPE_Y4M) {
1879 has_non_y4m_input = true;
1880 break;
1881 }
1882 }
1883 // Y4M reader has its own allocation.
1884 if (has_non_y4m_input) {
1885 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, width, height, 32)) {
1886 die("Failed to allocate image (%dx%d)", width, height);
1887 }
1888 }
1889
1891
1892 memcpy(&rc.layer_target_bitrate[0], &svc_params.layer_target_bitrate[0],
1893 sizeof(svc_params.layer_target_bitrate));
1894
1895 unsigned int total_rate = 0;
1896 for (i = 0; i < ss_number_layers; i++) {
1897 total_rate +=
1898 svc_params
1899 .layer_target_bitrate[i * ts_number_layers + ts_number_layers - 1];
1900 }
1901 if (total_rate != cfg.rc_target_bitrate) {
1902 die("Incorrect total target bitrate, expected: %d", total_rate);
1903 }
1904
1905 svc_params.framerate_factor[0] = 1;
1906 if (ts_number_layers == 2) {
1907 svc_params.framerate_factor[0] = 2;
1908 svc_params.framerate_factor[1] = 1;
1909 } else if (ts_number_layers == 3) {
1910 svc_params.framerate_factor[0] = 4;
1911 svc_params.framerate_factor[1] = 2;
1912 svc_params.framerate_factor[2] = 1;
1913 }
1914
1915 libaom_examples::MultilayerMetadata multilayer_metadata;
1916 if (app_input.multilayer_metadata_file != NULL) {
1917 if (!libaom_examples::parse_multilayer_file(
1918 app_input.multilayer_metadata_file, &multilayer_metadata)) {
1919 die("Failed to parse multilayer metadata");
1920 }
1921 libaom_examples::print_multilayer_metadata(multilayer_metadata);
1922 }
1923
1924 framerate = cfg.g_timebase.den / cfg.g_timebase.num;
1925 set_rate_control_metrics(&rc, framerate, ss_number_layers, ts_number_layers);
1926
1927 AvxVideoInfo info;
1928 info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
1929 info.frame_width = cfg.g_w;
1930 info.frame_height = cfg.g_h;
1931 info.time_base.numerator = cfg.g_timebase.num;
1932 info.time_base.denominator = cfg.g_timebase.den;
1933 // Open an output file for each stream.
1934 for (int sl = 0; sl < ss_number_layers; ++sl) {
1935 for (int tl = 0; tl < ts_number_layers; ++tl) {
1936 i = sl * ts_number_layers + tl;
1937 char file_name[PATH_MAX];
1938 snprintf(file_name, sizeof(file_name), "%s_%d.av1",
1939 app_input.output_filename, i);
1940 if (app_input.output_obu) {
1941 obu_files[i] = fopen(file_name, "wb");
1942 if (!obu_files[i]) die("Failed to open %s for writing", file_name);
1943 } else {
1944 outfile[i] = aom_video_writer_open(file_name, kContainerIVF, &info);
1945 if (!outfile[i]) die("Failed to open %s for writing", file_name);
1946 }
1947 }
1948 }
1949 if (app_input.output_obu) {
1950 total_layer_obu_file = fopen(app_input.output_filename, "wb");
1951 if (!total_layer_obu_file)
1952 die("Failed to open %s for writing", app_input.output_filename);
1953 } else {
1954 total_layer_file =
1955 aom_video_writer_open(app_input.output_filename, kContainerIVF, &info);
1956 if (!total_layer_file)
1957 die("Failed to open %s for writing", app_input.output_filename);
1958 }
1959
1960 // Initialize codec.
1961 aom_codec_ctx_t codec;
1962 aom_codec_flags_t flag = 0;
1964 flag |= app_input.show_psnr ? AOM_CODEC_USE_PSNR : 0;
1965 if (aom_codec_enc_init(&codec, encoder, &cfg, flag))
1966 die_codec(&codec, "Failed to initialize encoder");
1967
1968#if CONFIG_AV1_DECODER
1969 if (app_input.decode) {
1970 if (aom_codec_dec_init(&decoder, get_aom_decoder_by_index(0), NULL, 0))
1971 die_codec(&decoder, "Failed to initialize decoder");
1972 }
1973#endif
1974
1975 aom_codec_control(&codec, AOME_SET_CPUUSED, app_input.speed);
1976 aom_codec_control(&codec, AV1E_SET_AQ_MODE, app_input.aq_mode ? 3 : 0);
1991
1992 // Settings to reduce key frame encoding time.
1998
2000
2001 aom_codec_control(&codec, AV1E_SET_TUNE_CONTENT, app_input.tune_content);
2002 if (app_input.tune_content == AOM_CONTENT_SCREEN) {
2004 // INTRABC is currently disabled for rt mode, as it's too slow.
2006 }
2007
2008 if (app_input.use_external_rc) {
2010 }
2011
2013
2016
2018
2019 svc_params.number_spatial_layers = ss_number_layers;
2020 svc_params.number_temporal_layers = ts_number_layers;
2021 for (i = 0; i < ss_number_layers * ts_number_layers; ++i) {
2022 svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
2023 svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
2024 }
2025 // SET QUANTIZER PER LAYER, E.G FOR 2 SPATIAL LAYERS:
2026 // svc_params.max_quantizers[0] = 40;
2027 // svc_params.min_quantizers[0] = 40;
2028 // svc_params.max_quantizers[1] = 50;
2029 // svc_params.min_quantizers[1] = 50;
2030
2031 if (!app_input.scale_factors_explicitly_set) {
2032 for (i = 0; i < ss_number_layers; ++i) {
2033 svc_params.scaling_factor_num[i] = 1;
2034 svc_params.scaling_factor_den[i] = 1;
2035 }
2036 if (ss_number_layers == 2) {
2037 svc_params.scaling_factor_num[0] = 1;
2038 svc_params.scaling_factor_den[0] = 2;
2039 } else if (ss_number_layers == 3) {
2040 svc_params.scaling_factor_num[0] = 1;
2041 svc_params.scaling_factor_den[0] = 4;
2042 svc_params.scaling_factor_num[1] = 1;
2043 svc_params.scaling_factor_den[1] = 2;
2044 }
2045 }
2046 aom_codec_control(&codec, AV1E_SET_SVC_PARAMS, &svc_params);
2047 // TODO(aomedia:3032): Configure KSVC in fixed mode.
2048
2049 // This controls the maximum target size of the key frame.
2050 // For generating smaller key frames, use a smaller max_intra_size_pct
2051 // value, like 100 or 200.
2052 {
2053 const int max_intra_size_pct = 300;
2055 max_intra_size_pct);
2056 }
2057
2058 for (int lx = 0; lx < ts_number_layers * ss_number_layers; lx++) {
2059 cx_time_layer[lx] = 0;
2060 frame_cnt_layer[lx] = 0;
2061 }
2062
2063 std::unique_ptr<aom::AV1RateControlRTC> rc_api;
2064 if (app_input.use_external_rc) {
2065 const aom::AV1RateControlRtcConfig rc_cfg =
2066 create_rtc_rc_config(cfg, app_input);
2067 rc_api = aom::AV1RateControlRTC::Create(rc_cfg);
2068 }
2069
2070 frame_avail = 1;
2071 struct psnr_stats psnr_stream;
2072 memset(&psnr_stream, 0, sizeof(psnr_stream));
2073 while (frame_avail || got_data) {
2074 struct aom_usec_timer timer;
2075 frame_avail = read_frame(&(app_input.input_ctx[0]), &raw);
2076 // Loop over spatial layers.
2077 for (int slx = 0; slx < ss_number_layers; slx++) {
2078 if (slx > 0 && app_input.input_ctx[slx].filename != NULL) {
2079 const int previous_layer_frame_avail = frame_avail;
2080 frame_avail = read_frame(&(app_input.input_ctx[slx]), &raw);
2081 if (previous_layer_frame_avail != frame_avail) {
2082 die("Mismatch in number of frames between spatial layer input files");
2083 }
2084 }
2085
2086 aom_codec_iter_t iter = NULL;
2087 const aom_codec_cx_pkt_t *pkt;
2088 int reference_updated = 0;
2089 int layer = 0;
2090 // Flag for superframe whose base is key.
2091 int is_key_frame = (frame_cnt % cfg.kf_max_dist) == 0;
2092 // For flexible mode:
2093 if (app_input.layering_mode >= 0) {
2094 // Set the reference/update flags, layer_id, and reference_map
2095 // buffer index.
2096 set_layer_pattern(app_input.layering_mode, frame_cnt, &layer_id,
2097 &ref_frame_config, &ref_frame_comp_pred,
2098 &use_svc_control, slx, is_key_frame,
2099 (app_input.layering_mode == 10), app_input.speed,
2100 &reference_updated);
2101 aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
2102 if (use_svc_control) {
2104 &ref_frame_config);
2106 &ref_frame_comp_pred);
2107 }
2108 if (app_input.multilayer_metadata_file != NULL) {
2109 add_multilayer_metadata(&raw, multilayer_metadata, frame_cnt, slx);
2110 }
2111 // Set the speed per layer.
2112 if (test_speed_per_layer) {
2113 int speed_per_layer = 10;
2114 if (layer_id.spatial_layer_id == 0) {
2115 if (layer_id.temporal_layer_id == 0) speed_per_layer = 6;
2116 if (layer_id.temporal_layer_id == 1) speed_per_layer = 7;
2117 if (layer_id.temporal_layer_id == 2) speed_per_layer = 8;
2118 } else if (layer_id.spatial_layer_id == 1) {
2119 if (layer_id.temporal_layer_id == 0) speed_per_layer = 7;
2120 if (layer_id.temporal_layer_id == 1) speed_per_layer = 8;
2121 if (layer_id.temporal_layer_id == 2) speed_per_layer = 9;
2122 } else if (layer_id.spatial_layer_id == 2) {
2123 if (layer_id.temporal_layer_id == 0) speed_per_layer = 8;
2124 if (layer_id.temporal_layer_id == 1) speed_per_layer = 9;
2125 if (layer_id.temporal_layer_id == 2) speed_per_layer = 10;
2126 }
2127 aom_codec_control(&codec, AOME_SET_CPUUSED, speed_per_layer);
2128 }
2129 } else {
2130 // Only up to 3 temporal layers supported in fixed mode.
2131 // Only need to set spatial and temporal layer_id: reference
2132 // prediction, refresh, and buffer_idx are set internally.
2133 layer_id.spatial_layer_id = slx;
2134 layer_id.temporal_layer_id = 0;
2135 if (ts_number_layers == 2) {
2136 layer_id.temporal_layer_id = (frame_cnt % 2) != 0;
2137 } else if (ts_number_layers == 3) {
2138 if (frame_cnt % 2 != 0)
2139 layer_id.temporal_layer_id = 2;
2140 else if ((frame_cnt > 1) && ((frame_cnt - 2) % 4 == 0))
2141 layer_id.temporal_layer_id = 1;
2142 }
2143 aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
2144 }
2145
2146 if (set_err_resil_frame && cfg.g_error_resilient == 0) {
2147 // Set error_resilient per frame: off/0 for base layer and
2148 // on/1 for enhancement layer frames.
2149 // Note that this is can only be done on the fly/per-frame/layer
2150 // if the config error_resilience is off/0. See the logic for updating
2151 // in set_encoder_config():
2152 // tool_cfg->error_resilient_mode =
2153 // cfg->g_error_resilient | extra_cfg->error_resilient_mode;
2154 const int err_resil_mode =
2155 layer_id.spatial_layer_id > 0 || layer_id.temporal_layer_id > 0;
2157 err_resil_mode);
2158 }
2159
2160 layer = slx * ts_number_layers + layer_id.temporal_layer_id;
2161 if (frame_avail && slx == 0) ++rc.layer_input_frames[layer];
2162
2163 if (test_dynamic_scaling_single_layer) {
2164 // Example to scale source down by 2x2, then 4x4, and then back up to
2165 // 2x2, and then back to original.
2166 int frame_2x2 = 200;
2167 int frame_4x4 = 400;
2168 int frame_2x2up = 600;
2169 int frame_orig = 800;
2170 if (frame_cnt >= frame_2x2 && frame_cnt < frame_4x4) {
2171 // Scale source down by 2x2.
2172 struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
2173 aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
2174 } else if (frame_cnt >= frame_4x4 && frame_cnt < frame_2x2up) {
2175 // Scale source down by 4x4.
2176 struct aom_scaling_mode mode = { AOME_ONEFOUR, AOME_ONEFOUR };
2177 aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
2178 } else if (frame_cnt >= frame_2x2up && frame_cnt < frame_orig) {
2179 // Source back up to 2x2.
2180 struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
2181 aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
2182 } else if (frame_cnt >= frame_orig) {
2183 // Source back up to original resolution (no scaling).
2184 struct aom_scaling_mode mode = { AOME_NORMAL, AOME_NORMAL };
2185 aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
2186 }
2187 if (frame_cnt == frame_2x2 || frame_cnt == frame_4x4 ||
2188 frame_cnt == frame_2x2up || frame_cnt == frame_orig) {
2189 // For dynamic resize testing on single layer: refresh all references
2190 // on the resized frame: this is to avoid decode error:
2191 // if resize goes down by >= 4x4 then libaom decoder will throw an
2192 // error that some reference (even though not used) is beyond the
2193 // limit size (must be smaller than 4x4).
2194 for (i = 0; i < REF_FRAMES; i++) ref_frame_config.refresh[i] = 1;
2195 if (use_svc_control) {
2197 &ref_frame_config);
2199 &ref_frame_comp_pred);
2200 }
2201 }
2202 }
2203
2204 // Change target_bitrate every other frame.
2205 if (test_changing_bitrate && frame_cnt % 2 == 0) {
2206 if (frame_cnt < 500)
2207 cfg.rc_target_bitrate += 10;
2208 else
2209 cfg.rc_target_bitrate -= 10;
2210 // Do big increase and decrease.
2211 if (frame_cnt == 100) cfg.rc_target_bitrate <<= 1;
2212 if (frame_cnt == 600) cfg.rc_target_bitrate >>= 1;
2213 if (cfg.rc_target_bitrate < 100) cfg.rc_target_bitrate = 100;
2214 // Call change_config, or bypass with new control.
2215 // res = aom_codec_enc_config_set(&codec, &cfg);
2217 cfg.rc_target_bitrate))
2218 die_codec(&codec, "Failed to SET_BITRATE_ONE_PASS_CBR");
2219 }
2220
2221 if (rc_api) {
2222 aom::AV1FrameParamsRTC frame_params;
2223 // TODO(jianj): Add support for SVC.
2224 frame_params.spatial_layer_id = 0;
2225 frame_params.temporal_layer_id = 0;
2226 frame_params.frame_type =
2227 is_key_frame ? aom::kKeyFrame : aom::kInterFrame;
2228 rc_api->ComputeQP(frame_params);
2229 const int current_qp = rc_api->GetQP();
2231 qindex_to_quantizer(current_qp))) {
2232 die_codec(&codec, "Failed to SET_QUANTIZER_ONE_PASS");
2233 }
2234 }
2235
2236 if (test_active_maps) set_active_map(&cfg, &codec, frame_cnt);
2237
2238 // Do the layer encode.
2239 aom_usec_timer_start(&timer);
2240 if (aom_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags))
2241 die_codec(&codec, "Failed to encode frame");
2242 aom_usec_timer_mark(&timer);
2243 cx_time += aom_usec_timer_elapsed(&timer);
2244 cx_time_layer[layer] += aom_usec_timer_elapsed(&timer);
2245 frame_cnt_layer[layer] += 1;
2246
2247 // Get the high motion content flag.
2248 int content_flag = 0;
2250 &content_flag)) {
2251 die_codec(&codec, "Failed to GET_HIGH_MOTION_CONTENT_SCREEN_RTC");
2252 }
2253
2254 got_data = 0;
2255 // For simulcast (mode 11): write out each spatial layer to the file.
2256 int ss_layers_write = (app_input.layering_mode == 11)
2257 ? layer_id.spatial_layer_id + 1
2258 : ss_number_layers;
2259 while ((pkt = aom_codec_get_cx_data(&codec, &iter))) {
2260 switch (pkt->kind) {
2262 for (int sl = layer_id.spatial_layer_id; sl < ss_layers_write;
2263 ++sl) {
2264 for (int tl = layer_id.temporal_layer_id; tl < ts_number_layers;
2265 ++tl) {
2266 int j = sl * ts_number_layers + tl;
2267 if (app_input.output_obu) {
2268 fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
2269 obu_files[j]);
2270 } else {
2271 aom_video_writer_write_frame(
2272 outfile[j],
2273 reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
2274 pkt->data.frame.sz, pts);
2275 }
2276 if (sl == layer_id.spatial_layer_id)
2277 rc.layer_encoding_bitrate[j] += 8.0 * pkt->data.frame.sz;
2278 }
2279 }
2280 got_data = 1;
2281 // Write everything into the top layer.
2282 if (app_input.output_obu) {
2283 fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
2284 total_layer_obu_file);
2285 } else {
2286 aom_video_writer_write_frame(
2287 total_layer_file,
2288 reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
2289 pkt->data.frame.sz, pts);
2290 }
2291 // Keep count of rate control stats per layer (for non-key).
2292 if (!(pkt->data.frame.flags & AOM_FRAME_IS_KEY)) {
2293 int j = layer_id.spatial_layer_id * ts_number_layers +
2294 layer_id.temporal_layer_id;
2295 assert(j >= 0);
2296 rc.layer_avg_frame_size[j] += 8.0 * pkt->data.frame.sz;
2297 rc.layer_avg_rate_mismatch[j] +=
2298 fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[j]) /
2299 rc.layer_pfb[j];
2300 if (slx == 0) ++rc.layer_enc_frames[layer_id.temporal_layer_id];
2301 }
2302
2303 if (rc_api) {
2304 rc_api->PostEncodeUpdate(pkt->data.frame.sz);
2305 }
2306 // Update for short-time encoding bitrate states, for moving window
2307 // of size rc->window, shifted by rc->window / 2.
2308 // Ignore first window segment, due to key frame.
2309 // For spatial layers: only do this for top/highest SL.
2310 if (frame_cnt > rc.window_size && slx == ss_number_layers - 1) {
2311 sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
2312 rc.window_size = (rc.window_size <= 0) ? 1 : rc.window_size;
2313 if (frame_cnt % rc.window_size == 0) {
2314 rc.window_count += 1;
2315 rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
2316 rc.variance_st_encoding_bitrate +=
2317 (sum_bitrate / rc.window_size) *
2318 (sum_bitrate / rc.window_size);
2319 sum_bitrate = 0.0;
2320 }
2321 }
2322 // Second shifted window.
2323 if (frame_cnt > rc.window_size + rc.window_size / 2 &&
2324 slx == ss_number_layers - 1) {
2325 sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
2326 if (frame_cnt > 2 * rc.window_size &&
2327 frame_cnt % rc.window_size == 0) {
2328 rc.window_count += 1;
2329 rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
2330 rc.variance_st_encoding_bitrate +=
2331 (sum_bitrate2 / rc.window_size) *
2332 (sum_bitrate2 / rc.window_size);
2333 sum_bitrate2 = 0.0;
2334 }
2335 }
2336
2337#if CONFIG_AV1_DECODER
2338 if (app_input.decode) {
2339 if (aom_codec_decode(
2340 &decoder,
2341 reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
2342 pkt->data.frame.sz, NULL))
2343 die_codec(&decoder, "Failed to decode frame");
2344 }
2345#endif
2346
2347 break;
2348 case AOM_CODEC_PSNR_PKT:
2349 if (app_input.show_psnr) {
2350 psnr_stream.psnr_sse_total[0] += pkt->data.psnr.sse[0];
2351 psnr_stream.psnr_samples_total[0] += pkt->data.psnr.samples[0];
2352 for (int plane = 0; plane < 4; plane++) {
2353 psnr_stream.psnr_totals[0][plane] += pkt->data.psnr.psnr[plane];
2354 }
2355 psnr_stream.psnr_count[0]++;
2356 }
2357 break;
2358 default: break;
2359 }
2360 }
2361#if CONFIG_AV1_DECODER
2362 if (got_data && app_input.decode) {
2363 // Don't look for mismatch on non reference frames.
2364 if (reference_updated) {
2365 if (test_decode(&codec, &decoder, frame_cnt)) {
2366#if CONFIG_INTERNAL_STATS
2367 fprintf(stats_file, "First mismatch occurred in frame %d\n",
2368 frame_cnt);
2369 fclose(stats_file);
2370#endif
2371 fatal("Mismatch seen");
2372 }
2373 }
2374 }
2375#endif
2376 } // loop over spatial layers
2377 ++frame_cnt;
2378 pts += frame_duration;
2379 }
2380
2381 for (i = 0; i < MAX_NUM_SPATIAL_LAYERS; ++i) {
2382 if (app_input.input_ctx[i].filename == NULL) {
2383 break;
2384 }
2385 close_input_file(&(app_input.input_ctx[i]));
2386 }
2387 printout_rate_control_summary(&rc, frame_cnt, ss_number_layers,
2388 ts_number_layers);
2389
2390 printf("\n");
2391 for (int slx = 0; slx < ss_number_layers; slx++)
2392 for (int tlx = 0; tlx < ts_number_layers; tlx++) {
2393 int lx = slx * ts_number_layers + tlx;
2394 printf("Per layer encoding time/FPS stats for encoder: %d %d %d %f %f \n",
2395 slx, tlx, frame_cnt_layer[lx],
2396 (float)cx_time_layer[lx] / (double)(frame_cnt_layer[lx] * 1000),
2397 1000000 * (double)frame_cnt_layer[lx] / (double)cx_time_layer[lx]);
2398 }
2399
2400 printf("\n");
2401 printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f\n",
2402 frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
2403 1000000 * (double)frame_cnt / (double)cx_time);
2404
2405 if (app_input.show_psnr) {
2406 show_psnr(&psnr_stream, 255.0);
2407 }
2408
2409 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy encoder");
2410
2411#if CONFIG_AV1_DECODER
2412 if (app_input.decode) {
2413 if (aom_codec_destroy(&decoder))
2414 die_codec(&decoder, "Failed to destroy decoder");
2415 }
2416#endif
2417
2418#if CONFIG_INTERNAL_STATS
2419 fprintf(stats_file, "No mismatch detected in recon buffers\n");
2420 fclose(stats_file);
2421#endif
2422
2423 // Try to rewrite the output file headers with the actual frame count.
2424 for (i = 0; i < ss_number_layers * ts_number_layers; ++i)
2425 aom_video_writer_close(outfile[i]);
2426 aom_video_writer_close(total_layer_file);
2427
2428 if (has_non_y4m_input) {
2429 aom_img_free(&raw);
2430 }
2431 return EXIT_SUCCESS;
2432}
Describes the decoder algorithm interface to applications.
Describes the encoder algorithm interface to applications.
Describes the aom image descriptor and associated operations.
@ AOM_MIF_KEY_FRAME
Definition aom_image.h:176
@ AOM_MIF_ANY_FRAME_LAYER_SPECIFIC
Definition aom_image.h:183
@ AOM_CSP_UNKNOWN
Definition aom_image.h:143
enum aom_chroma_sample_position aom_chroma_sample_position_t
List of chroma sample positions.
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition aom_image.h:38
aom_image_t * aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
@ AOM_IMG_FMT_I420
Definition aom_image.h:45
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
int aom_img_add_metadata(aom_image_t *img, uint32_t type, const uint8_t *data, size_t sz, aom_metadata_insert_flags_t insert_flag)
Add metadata to image.
void aom_img_free(aom_image_t *img)
Close an image descriptor.
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
#define AOM_MAX_LAYERS
Definition aomcx.h:1738
#define AOM_MAX_TS_LAYERS
Definition aomcx.h:1740
aom_codec_iface_t * aom_codec_av1_cx(void)
The interface to the AV1 encoder.
@ AOM_FULL_SUPERFRAME_DROP
Definition aomcx.h:1812
@ AV1E_SET_BITRATE_ONE_PASS_CBR
Codec control to set the target bitrate in kilobits per second, unsigned int parameter....
Definition aomcx.h:1540
@ AV1E_SET_ENABLE_SMOOTH_INTRA
Codec control function to turn on / off smooth intra modes usage, int parameter.
Definition aomcx.h:1081
@ AV1E_SET_ENABLE_TPL_MODEL
Codec control function to enable RDO modulated by frame temporal dependency, unsigned int parameter.
Definition aomcx.h:418
@ AV1E_SET_AQ_MODE
Codec control function to set adaptive quantization mode, unsigned int parameter.
Definition aomcx.h:478
@ AV1E_SET_SVC_LAYER_ID
Codec control function to set the layer id, aom_svc_layer_id_t* parameter.
Definition aomcx.h:1289
@ AV1E_SET_SVC_REF_FRAME_CONFIG
Codec control function to set the reference frame config, aom_svc_ref_frame_config_t* parameter.
Definition aomcx.h:1299
@ AV1E_SET_TUNE_CONTENT
Codec control function to set content type, aom_tune_content parameter.
Definition aomcx.h:507
@ AV1E_SET_CDF_UPDATE_MODE
Codec control function to set CDF update mode, unsigned int parameter.
Definition aomcx.h:516
@ AV1E_SET_ENABLE_ANGLE_DELTA
Codec control function to turn on/off intra angle delta, int parameter.
Definition aomcx.h:1128
@ AV1E_SET_MV_COST_UPD_FREQ
Control to set frequency of the cost updates for motion vectors, unsigned int parameter.
Definition aomcx.h:1267
@ AV1E_SET_INTRA_DEFAULT_TX_ONLY
Control to use default tx type only for intra modes, int parameter.
Definition aomcx.h:1216
@ AV1E_SET_SVC_REF_FRAME_COMP_PRED
Codec control function to set reference frame compound prediction. aom_svc_ref_frame_comp_pred_t* par...
Definition aomcx.h:1404
@ AV1E_SET_ENABLE_INTRABC
Codec control function to turn on/off intra block copy mode, int parameter.
Definition aomcx.h:1124
@ AV1E_SET_ENABLE_WARPED_MOTION
Codec control function to turn on / off warped motion usage at sequence level, int parameter.
Definition aomcx.h:1049
@ AV1E_SET_RTC_EXTERNAL_RC
Codec control function to set flag for rate control used by external encoders.
Definition aomcx.h:1439
@ AV1E_SET_COEFF_COST_UPD_FREQ
Control to set frequency of the cost updates for coefficients, unsigned int parameter.
Definition aomcx.h:1247
@ AV1E_SET_ENABLE_CDEF
Codec control function to encode with CDEF, unsigned int parameter.
Definition aomcx.h:681
@ AOME_SET_ACTIVEMAP
Codec control function to pass an Active map to encoder, aom_active_map_t* parameter.
Definition aomcx.h:190
@ AV1E_SET_DV_COST_UPD_FREQ
Control to set frequency of the cost updates for intrabc motion vectors, unsigned int parameter.
Definition aomcx.h:1370
@ AV1E_SET_SVC_FRAME_DROP_MODE
Codec control to set the frame drop mode for SVC, unsigned int parameter. The valid values are consta...
Definition aomcx.h:1553
@ AV1E_SET_SVC_PARAMS
Codec control function to set SVC parameters, aom_svc_params_t* parameter.
Definition aomcx.h:1294
@ AV1E_SET_ENABLE_FILTER_INTRA
Codec control function to turn on / off filter intra usage at sequence level, int parameter.
Definition aomcx.h:1070
@ AV1E_SET_ENABLE_PALETTE
Codec control function to turn on/off palette mode, int parameter.
Definition aomcx.h:1120
@ AV1E_SET_ENABLE_CFL_INTRA
Codec control function to turn on / off CFL uv intra mode usage, int parameter.
Definition aomcx.h:1099
@ AOME_SET_MAX_INTRA_BITRATE_PCT
Codec control function to set max data rate for intra frames, unsigned int parameter.
Definition aomcx.h:312
@ AV1E_SET_ERROR_RESILIENT_MODE
Codec control function to enable error_resilient_mode, int parameter.
Definition aomcx.h:452
@ AV1E_SET_ENABLE_OBMC
Codec control function to predict with OBMC mode, unsigned int parameter.
Definition aomcx.h:708
@ AV1E_SET_AUTO_TILES
Codec control to set auto tiling, unsigned int parameter. Value of 1 means encoder will set number of...
Definition aomcx.h:1561
@ AV1E_SET_LOOPFILTER_CONTROL
Codec control to control loop filter.
Definition aomcx.h:1419
@ AOME_SET_SCALEMODE
Codec control function to set encoder scaling mode for the next frame to be coded,...
Definition aomcx.h:197
@ AV1E_SET_ENABLE_ORDER_HINT
Codec control function to turn on / off frame order hint (int parameter). Affects: joint compound mod...
Definition aomcx.h:876
@ AV1E_SET_DELTAQ_MODE
Codec control function to set the delta q mode, unsigned int parameter.
Definition aomcx.h:1144
@ AV1E_SET_POSTENCODE_DROP_RTC
Codec control to enable post encode frame drop for RTC encoding, int parameter.
Definition aomcx.h:1577
@ AV1E_SET_ENABLE_GLOBAL_MOTION
Codec control function to turn on / off global motion usage for a sequence, int parameter.
Definition aomcx.h:1039
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings, int parameter.
Definition aomcx.h:220
@ AV1E_GET_HIGH_MOTION_CONTENT_SCREEN_RTC
Codec control to get the high motion content flag, used for screen content realtime (RTC) encoding,...
Definition aomcx.h:1568
@ AV1E_SET_GF_CBR_BOOST_PCT
Boost percentage for Golden Frame in CBR mode, unsigned int parameter.
Definition aomcx.h:349
@ AV1E_SET_QUANTIZER_ONE_PASS
Codec control to set quantizer for the next frame, int parameter.
Definition aomcx.h:1502
@ AV1E_SET_MODE_COST_UPD_FREQ
Control to set frequency of the cost updates for mode, unsigned int parameter.
Definition aomcx.h:1257
@ AV1E_SET_MAX_CONSEC_FRAME_DROP_MS_CBR
Codec control to set the maximum number of consecutive frame drops, in units of time (milliseconds),...
Definition aomcx.h:1583
@ AV1_GET_NEW_FRAME_IMAGE
Codec control function to get a pointer to the new frame.
Definition aom.h:70
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
enum aom_bit_depth aom_bit_depth_t
Bit depth for codecThis enumeration determines the bit depth of the codec.
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
long aom_codec_flags_t
Initialization-time Feature Enabling.
Definition aom_codec.h:232
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition aom_codec.h:271
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
const char * aom_codec_err_to_string(aom_codec_err_t err)
Convert error number to printable string.
aom_codec_err_t
Algorithm return codes.
Definition aom_codec.h:155
#define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data)
aom_codec_control wrapper macro (adds type-checking, less flexible)
Definition aom_codec.h:542
const void * aom_codec_iter_t
Iterator.
Definition aom_codec.h:305
#define AOM_FRAME_IS_KEY
Definition aom_codec.h:288
@ AOM_BITS_8
Definition aom_codec.h:336
@ AOM_BITS_10
Definition aom_codec.h:337
@ AOM_CODEC_INVALID_PARAM
An application-supplied parameter is not valid.
Definition aom_codec.h:200
@ AOM_CODEC_MEM_ERROR
Memory operation failed.
Definition aom_codec.h:163
@ AOM_CODEC_OK
Operation completed without error.
Definition aom_codec.h:157
aom_codec_err_t aom_codec_decode(aom_codec_ctx_t *ctx, const uint8_t *data, size_t data_sz, void *user_priv)
Decode data.
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition aom_decoder.h:129
const aom_codec_cx_pkt_t * aom_codec_get_cx_data(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Encoded data iterator.
aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned long duration, aom_enc_frame_flags_t flags)
Encode a frame.
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition aom_encoder.h:943
aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface, aom_codec_enc_cfg_t *cfg, unsigned int usage)
Get the default configuration for a usage.
#define AOM_USAGE_REALTIME
usage parameter analogous to AV1 REALTIME mode.
Definition aom_encoder.h:1016
#define AOM_CODEC_USE_HIGHBITDEPTH
Definition aom_encoder.h:80
#define AOM_CODEC_USE_PSNR
Initialization-time Feature Enabling.
Definition aom_encoder.h:79
@ AOM_CBR
Definition aom_encoder.h:187
@ AOM_KF_AUTO
Definition aom_encoder.h:202
@ AOM_CODEC_PSNR_PKT
Definition aom_encoder.h:113
@ AOM_CODEC_CX_FRAME_PKT
Definition aom_encoder.h:110
aom active region map
Definition aomcx.h:1646
unsigned int rows
Definition aomcx.h:1649
unsigned int cols
Definition aomcx.h:1650
unsigned char * active_map
specify an on (1) or off (0) each 16x16 region within a frame
Definition aomcx.h:1648
Codec context structure.
Definition aom_codec.h:315
Encoder output packet.
Definition aom_encoder.h:122
size_t sz
Definition aom_encoder.h:127
enum aom_codec_cx_pkt_kind kind
Definition aom_encoder.h:123
double psnr[4]
Definition aom_encoder.h:145
union aom_codec_cx_pkt::@1 data
struct aom_codec_cx_pkt::@1::@2 frame
aom_codec_frame_flags_t flags
Definition aom_encoder.h:132
void * buf
Definition aom_encoder.h:126
Encoder configuration structure.
Definition aom_encoder.h:387
unsigned int g_input_bit_depth
Bit-depth of the input frames.
Definition aom_encoder.h:475
unsigned int rc_dropframe_thresh
Temporal resampling configuration, if supported by the codec.
Definition aom_encoder.h:540
struct aom_rational g_timebase
Stream timebase units.
Definition aom_encoder.h:489
unsigned int g_usage
Algorithm specific "usage" value.
Definition aom_encoder.h:399
unsigned int rc_buf_sz
Decoder Buffer Size.
Definition aom_encoder.h:705
unsigned int g_h
Height of the frame.
Definition aom_encoder.h:435
enum aom_kf_mode kf_mode
Keyframe placement mode.
Definition aom_encoder.h:768
enum aom_rc_mode rc_end_usage
Rate control algorithm to use.
Definition aom_encoder.h:623
unsigned int g_threads
Maximum number of threads to use.
Definition aom_encoder.h:407
unsigned int kf_min_dist
Keyframe minimum interval.
Definition aom_encoder.h:777
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition aom_encoder.h:518
unsigned int rc_buf_initial_sz
Decoder Buffer Initial Size.
Definition aom_encoder.h:714
unsigned int g_profile
Bitstream profile to use.
Definition aom_encoder.h:417
aom_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition aom_encoder.h:467
unsigned int g_w
Width of the frame.
Definition aom_encoder.h:426
unsigned int rc_undershoot_pct
Rate control adaptation undershoot control.
Definition aom_encoder.h:681
unsigned int kf_max_dist
Keyframe maximum interval.
Definition aom_encoder.h:786
aom_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition aom_encoder.h:497
unsigned int rc_max_quantizer
Maximum (Worst Quality) Quantizer.
Definition aom_encoder.h:668
unsigned int rc_buf_optimal_sz
Decoder Buffer Optimal Size.
Definition aom_encoder.h:723
unsigned int rc_min_quantizer
Minimum (Best Quality) Quantizer.
Definition aom_encoder.h:658
unsigned int rc_target_bitrate
Target data rate.
Definition aom_encoder.h:644
unsigned int rc_resize_mode
Mode for spatial resampling, if supported by the codec.
Definition aom_encoder.h:549
unsigned int rc_overshoot_pct
Rate control adaptation overshoot control.
Definition aom_encoder.h:690
Image Descriptor.
Definition aom_image.h:198
aom_img_fmt_t fmt
Definition aom_image.h:199
unsigned int d_w
Definition aom_image.h:213
unsigned int d_h
Definition aom_image.h:214
int num
Definition aom_encoder.h:165
int den
Definition aom_encoder.h:166
aom image scaling mode
Definition aomcx.h:1658
Struct for spatial and temporal layer ID.
Definition aomcx.h:1743
int temporal_layer_id
Definition aomcx.h:1745
int spatial_layer_id
Definition aomcx.h:1744
Parameter type for SVC.
Definition aomcx.h:1754
int max_quantizers[32]
Definition aomcx.h:1769
int number_spatial_layers
Definition aomcx.h:1761
int layer_target_bitrate[32]
Definition aomcx.h:1774
int framerate_factor[8]
Definition aomcx.h:1776
int min_quantizers[32]
Definition aomcx.h:1770
int scaling_factor_den[4]
Definition aomcx.h:1772
int number_temporal_layers
Definition aomcx.h:1768
int scaling_factor_num[4]
Definition aomcx.h:1771
Parameters for setting ref frame compound prediction.
Definition aomcx.h:1803
int use_comp_pred[3]
Definition aomcx.h:1806
Parameters for setting ref frame config.
Definition aomcx.h:1780
int reference[7]
Definition aomcx.h:1796
int refresh[8]
Definition aomcx.h:1799
int ref_idx[7]
Definition aomcx.h:1798